Martin C. Rinard
data[10]
- a buffer holding produced data.
num
- tells how many produced data items there are in the buffer.
Produce(v)
called when
producer produces data item v
. Consume(v)
called
when consumer is ready to consume a data item. Consumed item put
into v
.
bufferAvail
- signalled when a buffer becomes available.
dataAvail
- signalled when data becomes available.
monitor { Condition *bufferAvail, *dataAvail; int num = 0; int data[10]; Produce(v) { while (num == 10) { /* Mesa semantics */ bufferAvail->Wait(); } put v into data array num++; dataAvail->Signal(); /* must always do this? */ /* can replace with broadcast? */ } Consume(v) { while (num == 0) { /* Mesa Semantics */ dataAvail->Wait(); } put next data array value into v num--; bufferAvail->Signal(); /* must always do this? */ /* can replace with broadcast? */ } }
Condition *bufferAvail, *dataAvail; int num = 0; int data[10]; Lock *monitorLock; Produce(v) { monitorLock->Acquire(); /* Acquire monitor lock - makes operation atomic */ while (num == 10) { /* Mesa semantics */ bufferAvail->Wait(monitorLock); } put v into data array num++; dataAvail->Signal(monitorLock); /* must always do this? */ /* can replace with broadcast? */ monitorLock->Release(); /* Release monitor lock after perform operation */ } Consume(v) { monitorLock->Acquire(); /* Acquire monitor lock - makes operation atomic */ while (num == 0) { /* Mesa Semantics */ dataAvail->Wait(monitorLock); } put next data array value into v num--; bufferAvail->Signal(monitorLock); /* must always do this? */ /* can replace with broadcast? */ monitorLock->Release(); /* Release monitor lock after perform operation */ } }
Permission is granted to copy and distribute this material for educational purposes only, provided that the following credit line is included: "Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard." Permission is granted to alter and distribute this material provided that the following credit line is included: "Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard."