next up previous
Next: About this document Up: No Title Previous: Semaphores

Example : Producer Consumer

var mutex = 1 : semaphore ; {binary}
    space = N : semaphore ; 
    cans = 0  : semaphore ;

Process PRODUCER;
loop
  {pick can off crate}
  P(space) ; {wait for slot in the coke machine}
  P(mutex) ; {get mutual exclusion}
  {put one can in machine}
  V(mutex} ; {release mutual exclusion}
  V(cans} ; {signal cans available}
end ;

Process CONSUMER;
loop
 P(cans) ; {wait for any cans}
 P(mutex) ; {obtain mutual exclusion}
 {take one can}
 V(mutex) ; {release mutual exclusion}
 V(space) ; {signal space available}
end ;



Omer F Rana
Tue Feb 11 19:19:09 GMT 1997