Design an algorithm for a bounded-buffer monitor in which the buffers (portions) are embedded within the monitor itself."

Assume the buffer can hold MAX_ITEMS items and each item is an integer. You should write two functions: void produce(int v) - This function is called by the producer to put item v in the buffer int consume() - This function is called by the consumer to remove an item in the buffer and returns the item

Respuesta :

Answer:

Required code is given below:

Explanation:

monitor bounded buffer {

int items[MAX ITEMS];

int numItems = 0;

condition full, empty;

void produce(int v)

{

while (numItems == MAX ITEMS) full.wait();

items[numItems++] = v;

empty.signal();

}

int consume()

{

int retVal;

while (numItems == 0) empty.wait();

retVal = items[--numItems];

full.signal();

return retVal;

}

}

ACCESS MORE
EDU ACCESS
Universidad de Mexico