Coupon Accepted Successfully!


Bounded buffer problem

Bounded buffer problem is a classical synchronization problem for co-operating sequential processes. A perfect example of co-operating sequential processes is producer and consumer processes. Producer produces an item and the consumer consumes. In bounded buffer environment producer cannot produces items more than the size of buffer and consumer cannot consume items more than buffer size.
A shared memory solution to this problem exist which makes use of a shared variable counter initialized to 0. Producer can produce at max Buffer-1 items.
Code for Producer process


while (1) {
/* produce a newItem*/
while ( counter == Buffer_Size);
buffer [in] = newItem;
in = (in +1) % Buffer_Size;

counter ++;



Code for Consumer Process

while (1) {
while ( counter == 0);
item = buffer [out];
out = (out +1) % Buffer_Size;
counter --;
/* consume item */


Here buffer and counter variable is shared between the 2 processes and in and out are local variables for producer and consumer respectively.
Although both producer and consumer codes are correct separately but on concurrent execution may not produce correct result due to race condition as no proper synchronization mechanism exists.
Another solution to bounded buffer problem where there is no race condition exists. This solution makes use of semaphores. In this problem it is assumed that there exist n buffers each of size 1 instead of a single buffer of size n. Also, three semaphores are used. One is mutex used for providing mutual exclusion in critical section and it is initialized to 1.
The empty and full semaphores count the number of empty and full buffers, and are initialized to n and 0 respectively.
The structure of producer process is


do {
produce an item
add item to buffer

The structure of consumer process is

do {
remove item from buffer
consume the item

Test Your Skills Now!
Take a Quiz now
Reviewer Name