3 # prodcons.lsp - Producer
/consumer
5 # this program only runs on Linux
/UNIX
7 # usage of
'fork
', 'wait-pid
', 'semaphore
' and
'share
'
9 (if (> (& (last (sys-info)) 0xF) 4)
11 (println "this will not run on Win32")
15 (constant 'wait -
1 'signal
1 'release
0)
20 (semaphore cons-sem wait
)
21 (println (set 'i
(share data
)) " <-")
22 (semaphore prod-sem signal
))
27 (semaphore prod-sem wait
)
28 (println "-> " (share data i
))
29 (semaphore cons-sem signal
))
37 (set 'prod-sem
(semaphore)) ; get semaphores
38 (set 'cons-sem
(semaphore))
40 (set 'prod-pid
(fork (producer n
))) ; start threads
41 (set 'cons-pid
(fork (consumer n
)))
42 (semaphore prod-sem signal
) ; get producer started
44 (wait-pid prod-pid
) ; wait for threads to finish
46 (semaphore cons-sem release
) ; release semaphores
47 (semaphore prod-sem release
))