Show the content of buffer, in, and out for the following operations.
Assume that there are two operations; producer and consumer and the size of the buffer is 5.
You should draw the array on every operation.
Producer (“A”);
Producer (“B”);
X= Consumer ();
Y= Consumer ();
Producer (“C”);
Producer (“D”);
Producer (“E”);
X= Consumer ();
X= Consumer ();
Producer(“F”);
Y= Consumer();
Producer Consumer Problem:
Producer consumer problem is also called as Bounded buffer problem. It is the classic problem of synchronization. The problem is here we will have a n number of slots called a buffer capable of storing one unit of data in one slot and there works 2 operations called producer and consumer. The producer will add the data to the buffer and the consumer operation will remove the data from the buffer. While the producer will add the data buffer size increases and if it is consumer, the buffer size will decrease. The producer and the consumer should work independently, so that we use semaphores to solve this problem.
Here we use wait and signal operations for the semaphore problems. wait means putting the lock to not access others into buffer and singal means giving access to the other to enter into the buffer.
The producer operation will work as below:
do
{
wait(empty);
wait(mutex);
signal(mutex);
signal(full);
}
while(TRUE)
Here empty and mutex are the terms
that helps to work the producer operation in a better way.
wait(empty) will used to take count of the data in buffer which is
adding. wait(mutex) will make it locked and not allows the consemer
to not enter. signal(mutex) will unlock the lock and signal(full)
will increase the count of buffer after the producer comes out from
the buffer.
Consumer operation will
works as below:
do
{
wait(full);
wait(mutex);
signal(mutex);
signal(empty);
}
while(TRUE);
Here wait(full will hold the
producer to not change the buffer size while the consumer will be
there in the buffer. wait(mutex) will locks the buffer to not allow
the producer inside. signal(mutex) will unlock the buffer to give
access to the producer to enter into the buffer. signal(empty) will
decrease the buffer count after the consumer takes out the
data.
Given operations
are:
Producer (“A”);
Producer (“B”);
X= Consumer ();
Y= Consumer ();
Producer (“C”);
Producer (“D”);
Producer (“E”);
X= Consumer ();
X= Consumer ();
Producer(“F”);
Y= Consumer();
Below are the required array diagrams of given operations in the question.
From the above we can say that producer will produce from the left side of the array into buffer and consumer will take out the data from the right side of the array from the buffer. That is when producer produces data means it is considered as buffer in and the consumer consumes the data means it is considered as buffer out.
Hope the above is helpful. Please feel free to comment if any queries in the
comment section. I will try to solve them as soon as possible.
Do an up vote. Thank you..
Get Answers For Free
Most questions answered within 1 hours.