home *** CD-ROM | disk | FTP | other *** search
-
- CLI LISP Producer-Consumer Problem
-
-
- ;; The Producer-Consumer Problem
- ;; (synchronized)
- (defun pc ()
- (setf buffer nil)
- (setf information '(this is a test of semaphores))
- ; initializes the semaphores
- (initialize-semaphores '(($ok-to-consume 0) ($ok-to-produce 1)))
- ; starts concurrent reading and writing.
- (cobegin (list 'producer (length information))
- (list 'consumer (length information)))
- )
- (defun producer (r)
- (do ((i 0 (1+ i)))
- ((= i r) (print 'end-producer))
- ; start of critical region
- (wait '$ok-to-produce)
- (print 'read-by-producer<---)
- (setf buffer (nth i information))
- (princ buffer)
- (signal '$ok-to-consume)
- ; end of critical region
- )
- )
- (defun consumer (r)
- (do ((i 0 (1+ i)))
- ((= i r) (print 'end-consumer))
- ; start of critical region
- (wait '$ok-to-consume)
- (print '----print-by-consumer--->)
- (princ buffer)
- (setf buffer nil)
- (signal '$ok-to-produce)
- ; end of critical region
- )
- )
- ;
- ;; The Producer-Consumer Problem
- ;; (unsynchronized)
- (defun un-pc ()
- (setf buffer nil)
- (setf information '(this is a test of semaphores))
- ;; starts concurrent reading and writing.
- (cobegin (list 'un-producer (length information))
- (list 'un-consumer (length information)))
- )
- (defun un-producer (r)
- (do ((i 0 (1+ i)))
- ((= i r) (print 'end-producer))
- (print 'read-by-producer<---)
- (setf buffer (nth i information))
- (princ buffer)
- (terpri)
- )
- )
- (defun un-consumer (r)
- (do ((i 0 (1+ i)))
- ((= i r) (print 'end-consumer))
- (print '----print-by-consumer--->)
- (princ buffer)
- (terpri)
- (setf buffer nil)
- )
- )
- ;
-
-