Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

Session 3.6: Monitors

A language construct that makes mutual exclusion automatic: monitor structure, condition variables, signalling semantics, and a deadlock-free dining-philosophers solution.

Module 3 1 hour Theory CO3

Learning objectives

By the end of this session you should be able to:

  • Explain why semaphore-based code is error-prone with three examples.
  • Describe the structure of a monitor and how it guarantees mutual exclusion.
  • Use condition variables and contrast x.signal() with a semaphore signal.
  • Compare signal-and-wait with signal-and-continue semantics.
  • Trace the monitor solution to the dining-philosophers problem.

Lecture notes

1. Why semaphores are error-prone

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 before wait
signal(mutex);
  CS
wait(mutex);

Several processes in the CS at once.

wait instead of signal
wait(mutex);
  CS
wait(mutex);

Deadlock.

forgetting one
wait(mutex);
  CS
/* missing signal */

Everyone else waits forever.

2. The monitor

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 syntax
monitor monitor_name
{
    /* shared variable declarations */

    procedure P1 (...) { ... }
    procedure P2 (...) { ... }
    ...
    initialization_code (...) { ... }
}
entry queue: P3 P4 P5→ monitor: shared data + operations · one active process
condition x queue: P1condition y queue: P2

3. Condition variables

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).
Signal and wait (Hoare)

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.

Signal and continue (Mesa)

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.

4. Dining philosophers with a monitor (deadlock-free)

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
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.

5. Monitors in real languages

Java

Every object has a monitor: synchronized methods, with wait(), notify(), notifyAll().

Pthreads

pthread_mutex_t + pthread_cond_t: pthread_cond_wait(&c, &m) releases the mutex while waiting; pthread_cond_signal(&c).

C# / Python

lock / Monitor in C#; threading.Condition in Python.

Pthreads monitor pattern — always re-check the condition in a loop
pthread_mutex_lock(&m);
while (count == 0)                 /* Mesa semantics: re-check */
    pthread_cond_wait(&not_empty, &m);
/* ... remove an item ... */
pthread_mutex_unlock(&m);
Implementing a monitor with semaphores

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.

VideoBytes

Monitors and condition variables

Video coming soon

Monitors and condition variables

Recorded by Dr. Krithika L.B. Slot m3-monitors

Interactive lessons

Work through these in order after the lecture — each one expands a part of the notes with diagrams, hover notes and a quick check.

Lab connection

Check your understanding

Answer each question in your own words first, then reveal the model answer.

Revealed 0
What is the difference between x.signal() on a condition and signal(S) on a semaphore when no process is waiting?

x.signal() has no effect; signal(S) increments S, so a later wait(S) will not block — the signal is “remembered”.

Why must a Pthreads program call pthread_cond_wait inside a while loop?

Signal-and-continue semantics (and spurious wake-ups) mean the condition may be false again by the time the thread runs.

How does the monitor solution prevent deadlock among philosophers?

A philosopher eats only if both neighbours are not eating, taking both chopsticks at once, so hold-and-wait never happens.

Is a monitor more powerful than a semaphore?

No — each can implement the other. Monitors are safer and easier to use correctly.

Key takeaways

  • Monitors put shared data and operations together and allow one active process at a time — locking is automatic.
  • Condition variables: wait() suspends; signal() wakes one waiter or does nothing.
  • Hoare = signal-and-wait; Mesa/Pthreads/Java = signal-and-continue (re-check in a loop).

Where this leads

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 →