Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the Producer-Consumer problem The maximum queue size is 10. Consider th

ID: 3822912 • Letter: C

Question

Consider the Producer-Consumer problem The maximum queue size is 10. Consider the following solution. var semaphore s1=1, s2=1; itemtype A[11]; //size of array is one more than max queue size. Int f=0, r=0; /* items in the queue are from ((f+1) % 11) to r in a circular way. */ bool qempty() { return (f==r); } enque(itemtype item); { r= (x+1) % 11; A[r]= item; } bool qfull() { return (((r+1) % 11)==f); } function deque(itemtype & item); //function is called by reference. { f= (f+1) % 11; item= A[f]; } Code of a Producer ______ local var item: L1: produce(item); L2: P(s1); L3: while (qfull()); (*loop ends here*) L4: enque(item); L5: V(s1); L6: go to L1 Code of a Consumer ______ local var item: M1: P(s2); M2: while(qempty()); (*loop ends here*) M3: deque(item); M4: V(s2); M5: consume(item); M6: go to M1 (a) Is above code is 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 of 50 words or less. (b)To fbc the code, let us have a new semaphore s3. Initialize s3 and then add new statements in the code to make it work.

Explanation / Answer

a) The given code will not work. Because there is no check to determine lost wakeups. Thie above code could result in two or more processes reading or writing into the same slot at the same time.

b)

BufferSize = 10;

var semaphore s1 = 1, s2 = BufferSize, s3 = 0;

Producer()

{

local var item;

while (TRUE)

{

produce(item);

deque(s2); //decrement the s2 semaphore

deque(s1); //enter critical section

put_item(item); //put item in buffer

enque(s1); //leave critical section

enque(s3); //increment the s3 semaphore

}

}

Consumer()

{

local var item;

while(TRUE)

{

deque(s3); //decrement the s3 semaphore

deque(s1); //enter critical section

remove_item(item); //take an item from the buffer

enque(s1); //leave critical section

enque(s2); //increment the s2 semaphore

consume(item); //consume the item

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote