120-l6 Simplicity
120-l6 Simplicity
120-l6 Simplicity
Lecture 6:
Semaphores and Monitors
producer { consumer {
while (1) { while (1) {
Produce new resource; wait(full); // wait for a full buffer
wait(empty); // wait for empty buffer wait(mutex); // lock buffer list
wait(mutex); // lock buffer list Remove resource from a full buffer;
Add resource to an empty buffer; signal(mutex); // unlock buffer list
signal(mutex); // unlock buffer list signal(empty); // note an empty buffer
signal(full); // note a full buffer Consume resource;
} }
} }
Condition not_full;
…other variables… Waiting on
Condition not_empty; condition variables
void put_resource () {
…wait(not_full)…
…signal(not_empty)…
}
Resource get_resource () {
…
} Executing inside
} the monitor