This problem also known as bounded buffer problem.Here is its solution with binary semaphores:
Pseudocode:
Semaphore full , empty , mutex;
full=0;
empty=n;//n=number of resouces
mutex=1;
//s is a semaphore
Wait(s):
while(s<=0) do nothing;
s--;
Signal(s):
s++;
Producer():
produce_item();
Wait(empty);//empty--, n-1
Wait(mutex);//mutex=0
enter_Item();// We added item to the buffer
Signal(mutex);//mutex=1;
Signal(full);//full++;
Consumer():
wait(full); //full--
wait(mutex);//mutex--
remove_item();//removed item from the buffer
signal(mutex);// signal++
signal(empty);//empty++
consume_item();
The area between wait(mutex) and signal(mutex) also known as protected area because of mutual exclusion , when a process want to access its CS it can't.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment