Lecture 18
General
info on project tests and reports
return midterm and go over answers
Big Picture (see diagram on page 292)
await and << ... >> (chap 2)
spin locks and barriers (chap 3)
semaphores (chap 4)
monitors (chap 5) message passing (chap 7)
RPC and rendezvous (chap 8)
we will learn about more programming tools but our emphasis will now
change to focus more on program structures and interaction paradigms
Monitors
more structure than semaphores; more control than await
history: Hoare's 1974 paper
OS structure -- e.g., Unix
languages -- Mesa, ..., and now Java and Pthreads library
Basic Concepts
data encapsulation
implicit mutual exclusion
explicit signaling
monitor structure
monitor name {
declarations of permanent (static) variables
initialization code -- executes first
procedures (methods)
}
program structure
monitor1 ... monitorM
process1 ... processN
o processes interact indirectly by using the same monitor
o processes call monitor procedures
o at most one call active in a monitor at a time -- by definition
o explicit signaling using condition variables
o monitor invariant: predicate about local state that is true when no call is active
Condition Variables
cond cv; # queue of delayed processes; initially empty
wait(cv); # block on cv's queue AND release monitor lock
signal(cv); # awaken one process on cv's queue, if there is one
questions about signal:
which one to awaken? default is oldest (FIFO queue)
who executes next? the signaled process? or the signaler?
signaling disciplines:
signal and continue (SC) -- signaler goes next; used in Java, Unix, Pthreads
signal and wait (SW) -- signaled process goes next; used in Hoare's paper
SW is preemptive; SC is not
state diagram for synchronization in monitors -- Figure 5.1
[I develop it on the board, explaining as I go]
Examples of the Use of Monitors -- see Figures 5.2 and 5.3
monitor Semaphore {
int s = 0; ## s >= 0
cond pos; # signaled when s > 0
procedure Psem() {
while (s == 0) wait(pos);
s = s-1;
}
procedure Vsem() {
s = s+1;
signal(pos);
}
}
this code works for BOTH the SC and SW signaling disciplines. how?
but it is not FIFO for SC discipline. why?
variations:
suppose use if instead of while in Psem? -- works and is FIFO for SW only
suppose use double armed ifs in both procedures (see Figure 5.2) --
works and is FIFO for both SC and SW