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

The following partial code is a bounded-buffer monitor in which the buffers are

ID: 3861971 • Letter: T

Question

The following partial code is a bounded-buffer monitor in which the buffers are embedded within the monitor (with two condition variables). Assume any condition variable cond has two methods: cond.wait() and cond.signal(). Please implement the produce() and consume() methods in pseudo code (no need to have actual .c program). You cannot modify existing code and cannot have any additional synchronization mechanisms. monitor bounded_buffer { int items[MAX_ITEMS]; /* MAX_ITEM is a constant defined elsewhere */ int numItems = 0; /* # of items in the items array */ condition full, empty; void produce(int v); /* deposit the value v to the items array */ int consume(); /* remove the last item from items, and return the value */ }

Explanation / Answer

  

monitor bounded-buffer

{

int items[MAX_ITEMS];

int numitems=0;

condition full,empty;

procedure produce( int v)

{   

if(numitems==MAX_ITEMS) wait(full);// if buffer is full block

items[numitems]=v;//put item in buffer

numitems=numitems+1;//increment count

if(numitems==1) signal(empty);//if buffer is empty wake consumer

}

procedure int consume()

{

if(numitems==0) wait(empty);//if buffer is empty block

int x =items[numitems];//consume item

numitems=numitems-1;//decrement count

if(count==MAX_ITEMS -1) signa1(full);//if buffer is full,wake producer

return x;

end monitor

PRODUCER()

{

while (true)

{

int v;// item produced

bounded-buffer.produce(v);//call produce function in monitor

}

}

CONSUMER()

{

while (true)

{ int x;

x=bounded-buffer.consume();//the item consumed,call consume function in monitor

}

}