Print Friendly and PDF
Bounded buffer problem is a classical synchronization problem for cooperating sequential processes. A perfect example of co-operating sequential processes is producer and consumer processes. Producer produces an item and the consumer consumes.
Bounded buffer problem is a classical synchronization problem for cooperating 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 
Bounded buffer problem shared variable counter initialized to 0. Producer can produce at max Buffer-1 items.

Code for Producer process

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


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 */
}

Comment 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 The structure of consumer process is do 

{
produce an item
wait(empty);
wait(mutex);
add item to buffer
signal(mutex);
signaltfull);
}
while(1 );
do {
wait(full);
wait(mutex);
remove item from buffer
signal(mutex);
signal(empty);
consume the item
}
while(1 );
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: