Bounded Buffer Problem in Operating System

About Bounded Buffer Problem in OS

On this page, we will learn the concepts of Bounded Buffer problem in operating system.
Bounded buffer problem in operating system refers to the general case of the producer – consumer problem wherein the access is
controlled to a shared group of buffers of limited size.

 

Java Program to Convert Character to String and Vice-Versa

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.

Bound buffer problem

Problem

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

Readers Writer Wait and Signal Implementation

wait(Semaphore s)
{
  while(s == 0);
  s = s-1;
}

signal(Semaphore s)
{
  s = s+1;
}

Producer Buffer Solution

do
  {
    // 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

do 
{
    // 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);

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription