Assume that there are n buffers, each capable of holding a single item. We use three semaphores: empty and full to count the empty and full buffers and mutex to provide mutual exclusion for operations on the buffer pool.
mutex is initialized to 1, empty is initialized to n and full is initialized to 0.
Producer Process | Consumer Process |
---|---|
repeat         . . .         produce an item in nextp         . . .         wait(empty);         wait(mutex);         . . .         add nextp to buffer         . . .         signal(mutex);         signal(full); until false; |
repeat         wait(full);         wait(mutex);         . . .         remove item from buffer to nextc         . . .         signal(mutex);         signal(empty);         . . .         consume the item in nextc         . . . until false; |