Bounded Buffer Problem in Operating System (OS)

Bounded Buffer Problem in Operating System using Semaphores

In Bounded Buffer Problem there are three entities storage buffer slots, consumer and producer. The producer tries to store data in the storage slots while the consumer tries to remove the data from the buffer storage.

It is one of the most important process synchronizing problem let us understand more about the same.

Bounded Buffer Problem OS Operating System


The bounded buffer problem uses Semaphore. Please read more about Semaphores here before proceeding with this post here.

We need to make sure that the access to data buffer is only either to producer or consumer, i.e. when producer is placing the item in the buffer the consumer shouldn’t consume.

We do that via three entities –

  • Mutex mutex – used to lock and release critical section
  • empty – Keeps tab on number empty slots in the buffer at any given time
    • Initialised as n as all slots are empty.
  • full – Keeps tab on number of entities in buffer at any given time. 
    • Initialised as 0
Bounded Buffer signal and wait OS Operating System

Producer Buffer Solution

    // wait until empty > 0 and then decrement 'empty'
// that is there must be atleast 1 empty slot
wait(empty); // acquire the lock, so consumer can't enter wait(mutex); /* perform the insert operation in a slot */ // release lock signal(mutex); // increment 'full' signal(full); } while(TRUE)

Consumer Buffer Solution

    // wait until full > 0 and then decrement 'full'
// should be atleast 1 full slot in buffer
wait(full); // acquire the lock wait(mutex); /* perform the remove operation in a slot */ // release the lock signal(mutex); // increment 'empty' signal(empty); } while(TRUE);