Monitors and condition variables
Monitors and condition variables
Recorded by Dr. Krithika L.B. Slot m3-monitors
A language construct that makes mutual exclusion automatic: monitor structure, condition variables, signalling semantics, and a deadlock-free dining-philosophers solution.
By the end of this session you should be able to:
Semaphores work only if every process uses them correctly. A single mistake breaks the whole system, and such bugs appear only under particular interleavings — very hard to reproduce:
signal(mutex); CS wait(mutex);
Several processes in the CS at once.
wait(mutex); CS wait(mutex);
Deadlock.
wait(mutex); CS /* missing signal */
Everyone else waits forever.
A monitor (Hoare, Brinch Hansen, 1970s) is a high-level language construct — an abstract data type whose shared variables can be accessed only through its procedures, and in which only one process can be active at a time. The compiler inserts the locking, so the programmer cannot forget it.
monitor monitor_name
{
/* shared variable declarations */
procedure P1 (...) { ... }
procedure P2 (...) { ... }
...
initialization_code (...) { ... }
}Mutual exclusion alone is not enough — a process inside the monitor may need to wait for something (a buffer slot, both chopsticks). Monitors therefore provide condition variables with two operations:
x.wait() — the calling process is suspended (and leaves the monitor) until another process calls x.signal().x.signal() — resumes exactly one suspended process; if none is waiting, it has no effect
(unlike a semaphore's signal, which always increments).P signals Q; P waits until Q leaves the monitor (or waits on another condition). Q runs immediately, so the condition it waited for is still true.
P continues; Q runs later. The condition may have changed, so Q must re-check it in a while loop. Used by Pthreads and Java.
A philosopher picks up chopsticks only if both are available. Each philosopher is THINKING, HUNGRY or EATING; she may eat only if neither neighbour is eating.
monitor DiningPhilosophers
{
enum { THINKING, HUNGRY, EATING } state[5];
condition self[5];
void pickup(int i) {
state[i] = HUNGRY;
test(i); /* try to eat */
if (state[i] != EATING)
self[i].wait(); /* wait until a neighbour lets me */
}
void putdown(int i) {
state[i] = THINKING;
test((i + 4) % 5); /* left neighbour may eat now */
test((i + 1) % 5); /* right neighbour may eat now */
}
void test(int i) {
if (state[(i + 4) % 5] != EATING &&
state[i] == HUNGRY &&
state[(i + 1) % 5] != EATING) {
state[i] = EATING;
self[i].signal();
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
/* each philosopher i */
DiningPhilosophers.pickup(i);
/* eat */
DiningPhilosophers.putdown(i);P1 calls pickup(1): HUNGRY, test(1): neighbours P0 and P2 are not eating → P1 EATING.
P2 calls pickup(2): neighbour P1 is EATING → test fails → P2 does self[2].wait() and leaves the monitor.
P3: neighbours P2 (hungry, not eating) and P4 (thinking) → P3 EATING. P1 and P3 eat in parallel.
putdown(1): test(0) and test(2). P2's right neighbour P3 is still eating → P2 keeps waiting.
putdown(3): test(2) → P1 and P3 not eating → P2 becomes EATING and is signalled. No deadlock is possible, because chopsticks are taken both-or-none.
Deadlock-free, but a philosopher can still starve if her neighbours keep alternating.
Every object has a monitor: synchronized methods, with wait(), notify(), notifyAll().
pthread_mutex_t + pthread_cond_t: pthread_cond_wait(&c, &m) releases the mutex while waiting; pthread_cond_signal(&c).
lock / Monitor in C#; threading.Condition in Python.
pthread_mutex_lock(&m);
while (count == 0) /* Mesa semantics: re-check */
pthread_cond_wait(¬_empty, &m);
/* ... remove an item ... */
pthread_mutex_unlock(&m);A compiler can implement a monitor with a semaphore mutex = 1 around each procedure, a semaphore next = 0 on which signalling processes wait, and for each condition x a semaphore x_sem = 0 plus a counter x_count. So monitors are no more powerful than semaphores — just much safer to use.
Monitors and condition variables
Recorded by Dr. Krithika L.B. Slot m3-monitors
Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.
Answer each question in your own words first, then reveal the model answer.
Revealed 0x.signal() has no effect; signal(S) increments S, so a later wait(S) will not block — the signal is “remembered”.
Signal-and-continue semantics (and spurious wake-ups) mean the condition may be false again by the time the thread runs.
A philosopher eats only if both neighbours are not eating, taking both chopsticks at once, so hold-and-wait never happens.
No — each can implement the other. Monitors are safer and easier to use correctly.
Processes now cooperate safely. But each one also needs memory: how does the OS place many processes in RAM and keep them apart? Module 4.
Go to Module 4 — Session 4.1: Address binding and address spaces →