Semaphores and Monitors
Frédéric Haziza <daz@it.uu.se>
Department of Computer Systems
Uppsala University
Summer 2008
Semaphores Monitors Conclusion
From locks and barriers...
Are busy-waiting protocols complex?
No clear distinction between variables used:
•
for synchronization
•
for computing results
Busy-waiting is often inefficient
•
Usually more processes/threads than processors
•
Processor execu ting a spinning process can be more
productively e mployed to execute another process
Semaphores
First synchronisation tool (and remains one of the most important).
⇒ Easy to protect critical sections.
⇒ Included in (almost) all parallel programming libraries.
2 OS2’08 | Semaphores and Monitors
Semaphores Monitors Conclusion
Semaphore
Shared variable with 2 atomic methods:
down(Semaphore s) {
.Probeer (try) / Passeren (pass) / Pakken (grab)
< wait until c > 0, then c := c-1; >
.must be atomic once c > 0 is detected
}
up(Semaphore s) {
.Verhoog (increase)
< c = c + 1; > .must be atomic
}
Init(Semaphore s, Integer i){ c := i; }
4 OS2’08 | Semaphores and Monitors