Semaphores and Monitors
Semaphores and Monitors
Operating
Systems
Winter 2012
Semaphores and
Monitors
Mark
Zbikowski
Gary Kimura
Semaphores
Semaphore = a synchronization primitive
higher level of abstraction than locks
invented by Dijkstra in 1968, as part of the THE operating
system
A semaphore is:
a variable that is manipulated through two operations,
P and V (Dutch for test and increment)
P(sem) (wait/down)
block until sem > 0, then subtract 1 from sem and proceed
V(sem) (signal/up)
add 1 to sem
11/10/15
Blocking in semaphores
Each semaphore has an associated queue of threads
when P(sem) is called by a thread,
if sem was available (>0), decrement sem and let thread
continue
if sem was unavailable (<=0), place thread on associated
queue; dispatch some other runnable thread
Abstract implementation
P/wait/down(sem)
acquire real mutual exclusion
if sem is available (>0), decrement sem; release real mutual
exclusion; let thread continue
otherwise, place thread on associated queue; release real
mutual exclusion; run some other thread
V/signal/up(sem)
acquire real mutual exclusion
if thread(s) are waiting on the associated queue, unblock one
(place it on the ready queue)
if no threads are on the queue, sem is incremented
the signal is remembered for next time P(sem) is called
11/10/15
Counting semaphore
sem is initialized to N
N = number of units available
11/10/15
Usage
11/10/15
Pressing questions
11/10/15
tail
11/10/15
head
producer:
P(empty)
; one fewer buffer, block if none available
P(mutex)
; get access to pointers
<add item to buffer>
V(mutex)
; done with pointers
V(full)
; note one more full buffer
consumer:
P(full)
;wait until theres a full buffer
P(mutex)
;get access to pointers
<remove item from buffer>
V(mutex)
; done with pointers
V(empty)
; note theres an empty buffer
<use the item>
11/10/15
Note 1:
I have elided all the code
concerning which is the
first full buffer, which is the
last full buffer, etc.
Note 2:
Try to figure out how to do
this without using counting
semaphores!
Example: Readers/Writers
Description:
A single object is shared among several threads/processes
Sometimes a thread just reads the object
Sometimes a thread updates (writes) the object
We can allow multiple readers at a time
why?
11/10/15
10
writer:
P(wrt)
V(wrt)
reader:
P(mutex)
; ensure exclusion
readcount++
; one more reader
if readcount == 1 then P(wrt)
; if were the first, synch with writers
V(mutex)
<perform read operation>
P(mutex)
; ensure exclusion
readcount-; one fewer reader
if readcount == 0 then V(wrt)
; no more readers, allow a writer
V(mutex)
11/10/15
11
Readers/Writers notes
Notes:
the first reader blocks on P(wrt) if there is a writer
any other readers will then block on P(mutex)
11/10/15
12
11/10/15
13
14
A monitor encapsulates:
shared data structures
procedures that operate on the shared data
synchronization between concurrent threads that invoke those
procedures
Data can only be accessed from within the monitor, using the
provided procedures
protects the data from unstructured access
11/10/15
15
A monitor
shared data
operations (methods)
16
Monitor facilities
Automatic mutual exclusion
only one thread can be executing inside at any time
thus, synchronization is implicitly associated with the monitor it
comes for free
11/10/15
17
Produce()
Consume()
Buffer is empty
Now what?
11/10/15
18
Produce()
Consume()
Buffer is empty
Now what?
11/10/15
19
Condition variables
A place to wait; sometimes called a rendezvous point
Required for monitors
So useful theyre often provided even when monitors arent
available
signal(c)
wake up at most one waiting thread
if no waiting threads, signal is lost
this is different than semaphores: no history!
broadcast(c)
wake up all waiting threads
11/10/15
20
11/10/15
21
11/10/15
22
23
24
if (notReady) wait(c)
Mesa monitors:
11/10/15
25
11/10/15
26
Signal(c) {if someones waiting, step out and let him run}
if queue c is empty then put the caller on the ready queue
else move a thread from queue c to the ready queue, and put the
caller into queue m
choose someone to run
11/10/15
27
11/10/15
28
11/10/15
29
Monitor Summary
Language supports monitors
Compiler understands them
compiler inserts calls to runtime routines for
monitor entry
monitor exit
signal
Wait
30