/    /  OS – Bounded Buffer Problem

Bounded Buffer Problem

 

The bounded buffer problem or the producer-consumer problem is one of the classical problems of synchronization.

 

What is the Bounded Buffer Problem?

 

In the bounded buffer problem, there is a buffer of n slots, and each slot is capable of storing one unit of data. Producer and Consumer are the two processes operating on the Buffer.  

The producer tries to enter data in an empty slot, and the consumer tries to remove it. When the processes are run simultaneously, they will not give the expected results. 

There is a need for the producer and the consumer to run independently.

 

One solution to the Bounded Buffer problem is to use Semaphores. 

The Semaphores that are used are as follows:

  1. m, a Binary Semaphore.
  2. empty, a Counting Semaphore.
  3. full, a Counting Semaphore.

 

Production Operation:

 

The producer operation waits until there is at least one empty slot, then it decrements the Semaphore, as there will be one less empty slot.

Then it places the data in the empty slot. Hence, the buffer locks. After the data is inserted the value of full is incremented as the producer has filled a slot.

 

Consumer Operation:

 

The consumer operation waits until there is one full slot in the buffer. 

Then it decreases the value of the full Semaphore as the value will decrease after the completion of the consumer operation.

The consumer locks the buffer, then it removes the data from one slot. Following which it releases the lock. Then the empty Semaphore is incremented by one as there is one more empty slot.

 

Reference

Bounded Buffer Problem