Semaphores are powerful, but a single misplaced wait or signal can cause errors that are hard to find. Monitors give a safer, higher-level construct.
Monitors
A monitor, proposed by Hoare and Brinch Hansen, is a programming-language construct that groups shared data with the procedures that operate on it, and guarantees that only one process can be active inside the monitor at a time. Mutual exclusion is therefore automatic: the programmer need not write wait and signal for it. For waiting on conditions, a monitor has condition variables, such as x, with two operations: x.wait(), which suspends the calling process and releases the monitor, and x.signal(), which resumes exactly one process suspended on x, or does nothing if none is waiting. Java's synchronized methods with wait() and notify() work like monitors.
Definition: Monitor. A high-level synchronisation construct that encapsulates shared data and the procedures on it, allowing only one process at a time to execute within it, with condition variables for waiting.
The Dining-Philosophers Problem
Five philosophers sit at a round table, each with a plate, and there is one chopstick between each pair. A philosopher thinks, and when hungry picks up the two chopsticks on the left and right, eats, and puts them down. The problem models the allocation of several resources among several processes without deadlock or starvation. A simple solution gives each chopstick a semaphore, and each philosopher does wait(left); wait(right); eat; signal(left); signal(right). But if all five pick up their left chopsticks at the same moment, each waits for ever for the right one: a deadlock.
Solutions
Several remedies avoid deadlock: allow at most four philosophers at the table at once; let a philosopher pick up chopsticks only if both are free, inside a critical section; or make philosophers asymmetric, with odd ones picking left first and even ones right first. A monitor solution keeps a state for each philosopher (thinking, hungry, eating) and lets one eat only when neither neighbour is eating, using a condition variable for each. This avoids deadlock, although a philosopher might still starve without further care.
Other Classic Problems
The sleeping barber problem and the readers-writers problem are other classic tests for synchronisation methods.
Table 3.4: Synchronisation tools
| Tool | Level | Key operations |
|---|---|---|
| Mutex lock | Low | acquire, release |
| Semaphore | Low | wait (P), signal (V) |
| Monitor | High (language) | Procedures, x.wait, x.signal |
| Condition variable | Used in monitors | wait, signal |
Example 3.4: Deadlock among philosophers
Problem: Show how the simple semaphore solution can deadlock, and how the "both or none" rule prevents it.
Solution:
- All five become hungry together and each takes the left chopstick.
- Every right chopstick is now held by a neighbour, so all five wait: deadlock.
- With the rule, a philosopher takes chopsticks only when both are free, in one critical step.
- Then one philosopher whose neighbours are not eating takes both and eats, so progress is made.
Result: The "both or none" rule breaks the circular wait and prevents deadlock.
#include <stdio.h>
#include <pthread.h>
#define N 5
pthread_mutex_t chop[N];
int meals[N];
void *phil(void *arg) {
int i = *(int *)arg, l = i, r = (i + 1) % N;
int first = l < r ? l : r, second = l < r ? r : l;
for (int k = 0; k < 3; k++) {
pthread_mutex_lock(&chop[first]);
pthread_mutex_lock(&chop[second]);
meals[i]++;
pthread_mutex_unlock(&chop[second]);
pthread_mutex_unlock(&chop[first]);
}
return NULL;
}
int main(void) {
pthread_t t[N]; int id[N];
for (int i = 0; i < N; i++) pthread_mutex_init(&chop[i], NULL);
for (int i = 0; i < N; i++) {
id[i] = i; pthread_create(&t[i], NULL, phil, &id[i]);
}
for (int i = 0; i < N; i++) pthread_join(t[i], NULL);
for (int i = 0; i < N; i++)
printf("P%d ate %d times\n", i, meals[i]);
return 0;
}
Output
P0 ate 3 times
P1 ate 3 times
P2 ate 3 times
P3 ate 3 times
P4 ate 3 times
Common mistakes
- Thinking that a monitor's condition variable keeps a count like a semaphore. Signal with no waiter does nothing.
- Letting every philosopher pick up the left chopstick first, which can deadlock.
- Confusing freedom from deadlock with freedom from starvation.
Key takeaways
- A monitor lets only one process inside at a time.
- Condition variables use x.wait() and x.signal().
- The simple dining-philosophers solution can deadlock.
- Limiting diners, taking both chopsticks together, or asymmetric ordering prevent deadlock.
Practice questions
- What is a monitor?
- What does x.signal() do if no process is waiting on x?
- How can deadlock among the philosophers be avoided?
Answers to the practice questions
- A construct that groups shared data and its procedures and allows only one process inside at a time.
- Nothing.
- By allowing at most four at the table, taking both chopsticks only when both are free, or using an asymmetric order.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit III
