Process Coordination & Semaphores

The code we saw above is a pseudocode that contained only a part of each program.

The Producer program creates some item (number, string, or some other object) and places it inside the buffer array.

The Consumer program then reads the buffer's items, and processes them, one by one.

These programs will work without any issues. The only concern is that the maximum number of items which can be placed into the buffer is BUFFER_SIZE - 1. One slot is unavailable because there always has to be a gap between the producer and the consumer.

We could try to overcome this deficiency by changing the programs by introducing a counter variable, as shown in the following slide. The counter variable will also be shared by both programs (both the Producer and Consumer can access and change counter.)