COS 318: Operating Systems Semaphores, Monitors and Condition Variables
COS 318: Operating Systems Semaphores, Monitors and Condition Variables
Semaphores
COS 318: Operating Systems Monitors
Mesa-style monitors
Semaphores, Monitors and Programming idiom
Condition Variables
Kai Li
Computer Science Department
Princeton University
(http://www.cs.princeton.edu/courses/cos318/)
3
Implementation Motivation
procedures ...
Entry queue
procedures
Producer-Consumer with Monitors Options of the Signaler
Run the signaled thread immediately and suspend the
monitor ProdCons
condition full, empty;
current one (Hoare)
procedure Producer
begin
z If the signaler has other work to do, life is complex
procedure Enter;
begin
while true do z It is difficult to make sure there is nothing to do, because the
begin
if (buffer is full)
produce an item
signal implementation is not aware of how it is used
wait(full);
put item into buffer;
ProdCons.Enter(); z It is easy to prove things
end;
if (only one item)
signal(empty);
end; Exit the monitor (Hansen)
end;
procedure Consumer
z Signal must be the last statement of a monitor procedure
procedure Remove;
begin
while true do
Continues its execution (Mesa)
begin
if (buffer is empty)
begin z Easy to implement
ProdCons.Remove();
wait(empty);
consume an item; z But, the condition may not be true when the awaken process
remove an item;
if (buffer was full)
end; actually gets a chance to run
end;
signal(full);
end;
10
13 14
z Waking up:
• Acquire mutex
Can this be further improved?
• Resume
16
More on Mesa-Style Monitor Evolution of Monitors
Signaler continues execution Brinch-Hansen (73) and Hoare Monitor (74)
z Concept, but no implementation
Waiter simply put on ready queue, with no special z Requires Signal to be the last statement (Hansen)
priority z Requires relinquishing CPU to signaler (Hoare)
z Must then spin and reevaluate the condition Mesa Language (77)
z Monitor in language, but signaler keeps mutex and CPU
No constraints on when the waiting thread/process must z Waiter simply put on ready queue, with no special priority
run after a “signal” Modula-2+ (84) and Modula-3 (88)
Simple to introduce a broadcast: wake up all z Explicit LOCK primitive
z Mesa-style monitor
No need to make signal sticky Pthreads (95)
No constrains on signaler z Started standard effort around 1989
z Defined by ANSI/IEEE POSIX 1003.1 Runtime library
z Can execute after signal call (Hansen’s cannot)
Java threads
z Do not need to relinquish control to awaken thread/process z James Gosling in early 1990s without threads
z Use most of the Pthreads primitives
19 20
Example: Interrupt Handler Use Semaphore to Signal
A device thread works with an interrupt handler
What to do with shared data? Init(s,0);
What if “m” is held by another thread or by itself?
Device thread Interrupted Thread
while (1) {
Device thread Interrupt handler P(s); …
Acquire(m); Interrupt handler
... ... ... ... Interrupt
Acquire(); Acquire(); deal with interrupt V(s); …
...
Release();
...
? ...
Release();
...
}
...
Release(m);
...
21 22
Equivalence Summary
Semaphores Semaphores
z Good for signaling Monitors
z Not good for mutex because it is easy to introduce a bug z Hoare’s
Monitors z Mesa-style and its idiom
z Good for scheduling and mutex Examples
z Maybe costly for a simple signaling z Use cases for semaphores
23 24