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
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 010 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
