3. (30 pts) The following partial code is a bounded-buffer monitor in which the
ID: 3725590 • Letter: 3
Question
3. (30 pts) 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 ). There are multiple producers that invoke produce () and there are multiple consumers that invoke consume . Please implement the produce () and consume ) methods in C (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 num!tems = 0; /* # of items in the items array */ condition full, empty: 1 void produce (int v); deposit the value v to the items array/ int consume /* remove the last item from items, and return the value/ 5 1 5Explanation / Answer
produce(int v) {
full.wait(); //ask other producers to wait
items[numItems++] = v; //produce
empty.signal(); //informs consumers that array is nomore empty
if (numItems < MAX_ITEMS) //if more items can be produced
full.signal() //notify other producers
}
consume() {
empty.wait();
int n = items[--numItems];
full.signal();
if (numItems > 0)
empty.signal()
}
I hope this is clear!! If you need more clarifiacation, please let me know!!
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.