FOR FREE CONTENT

Solving the Producer-Consumer Problem using Semaphores:

 

Fig: 23 The producer-consumer problem using semaphores

 

This solution uses three semaphores: 

# Mutex is a binary semaphore used by the producer-consumer problem to access the buffer (critical section) in mutual exclusion manner i.e. producer and consumer do not access the buffer at the same time.

 

# Full is counting the semaphore variable for counting the number of slots that are full.

 

# Empty is counting the semaphore variable for counting the number of slots that are empty.

 

Full is initially 0, empty is initially equal to the number of slots in the buffer, and the mutex is initially 1. Semaphores that are initialized to 1 and used by two or more processes to ensure that only one of them can enter its critical region at the same time are called binary semaphores.