The producer-consumer problem

To illustrate a practical use of synchronization primitives, let us examine The Case Of The Producer Consumer Problem:

The essence of the problem is that one process is producing things (like, maybe decoding video from a file), and the other is consuming things (like, maybe displaying the decoded video on the screen. Between the producer and consumer is a fixed-size buffer

When the buffer is neither full nor empty, the producer may freely place items in the buffer, and the consumer may freely remove them. When the buffer is full, however, the producer must wait til the consumer removes some items. Similarly, when the buffer is empty, the consumer must wait until the producer has placed items in the buffer before it can remove any items.

Semaphores are used in one possible solution to the producer-consumer problem.


last updated 13 February 1998