

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Corner; Class: Operating Systems; Subject: Computer Science; University: University of Massachusetts - Amherst; Term: Spring 2009;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


CMPSCI 377 Operating Systems Spring 2009
Lecturer: Mark Corner Scribes: Bruno Silva,Jim Partan
Remember from last class that condition variables are synchronization primitives that enable threads to wait until a particular condition occurs.
Generalizing, the combination of locks and condition variables is sometimes called a monitor, which is sometimes incorporated into data structures in some languages (note that this terminology is not always used in a standardized way).
In the last class we also discussed how to use condition variables and signals to implement a simple producer- consumer system. Looking at this system again, could we move the signal() call in enqueue() down below the unlock()?
dequeue() lock(A) while (queue empty) wait(A, C) remove_item() unlock(A)
enqueue() lock(A) insert_item() // used to signal here, inside lock unlock(A) signal(C) // now signal here, outside lock
Will this work? The answer is yes. It might not be as clean conceptually, but nothing bad will happen (verify this for yourself).
As an additional note, signals are memoryless: If no threads are waiting for them, they don’t do anything.
Let us now discuss how to implement a more realistic producer-consumer, in the sense that it only has a bounded (finite) buffer. Assume we are dealing with a Coke machine: no one can buy a Coke while someone else is using the machine; the delivery person needs to wait until there is free space before putting more Coke(s) into the machine; and the buyer needs to wait until there is at least one Coke available. For this system, we are going to use two condition variables; the code is as follows:
9-2 Lecture 9: February 24
cokebuyer() lock(A) while (cokes==0) // machine is empty wait(A, coke_in_machine) buy_coke() // this works: when we get out of wait while loop, we are sure there is a Coke. signal(coke_bought) unlock(A)
deliveryperson() lock(A) while (cokes == maxcokes) // machine is full wait(A, coke_bought) put_coke_in_machine() // and now we are sure there is a spot to put a new Coke into the machine signal(coke_in_machine) unlock(A)