Consider the Producer-Consumer problem. The maximum queue size is 11. Consider t
ID: 3822916 • Letter: C
Question
Consider the Producer-Consumer problem.
The maximum queue size is 11.
Consider the following solution. Is this code correct? (YES or NO).
If the answer is NO, give a detailed scenario to exhibit the
incorrect behavior. If answer in YES, give an explanation in
50 words or less.
****HINT: The sequential computing aspect of the code is correct. You have to think
**** of problems arising from CONCURRENT PROCESSES.
semaphore s=1, s1=1, s2=1;
itemtype A[11]: //array index is 0…10
int n=0; /* no. of items in the queue.
n==0 means queue is empty.
n!=0 means queue is non-empty and the
items are in positions
f, (f+1) mod 11, (f+2) mod 11, ... n items.
We will try to make sure that (-1) < n < 12. */
int f=0; /* We will try to make sure that (-1) < f < 11.
*/
bool qempty(); bool qfull()
{ return (n==0);} {return (n==11);}
void enque(item, r) itemtype deque(){
{ A[r]=item; itemtype item;
n= n+1;} item= A[f];
f= f+1;
if (f==11) then f= 0;
n= n-1;
return item;}
Code of a Producer Code of a Consumer
------------------ ------------------
local var item; int r; local var item;
L1: produce(item);
L2: P(s1); M1: P(s2);
L3: while (qfull()); M2: while (qempty());
(*loop ends here*) (*loop ends here*)
r= f+n;
if (r>10) r=r-11;
//r is index of last
//item in queue.
P(s); P(s);
L4: enque(item, r); M3: deque(item);
V(s); V(s);
L5: V(s1) M4: V(s2);
L6: go to L1 M5: consume(item);
M6: go to M1
Explanation / Answer
Answer : Yes
Here, the Producer class is a thread which constantly produces objects and put them into the queue. In practice, we should specify condition to exit the loop, such as closing/shutdown the program or a maximum number of objects reached.
The Consumer class is another thread which constantly takes objects from the queue to process. In practice, we should specify condition to stop this thread by checking the queue for a special object.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.