Sub topic 14
Dining philosophers
This chapter assumes you know the semaphore operations wait and signal, the idea of a critical section, and the four deadlock conditions in outline. The dining philosophers problem tests whether you can share several resources among several processes without deadlock or starvation.
The problem
Five philosophers sit at a round table. Between each pair of neighbours lies one fork, so there are five forks. A philosopher alternates between thinking and eating. To eat, a philosopher needs both the left and the right fork. Forks are picked up one at a time, and a fork can be held by only one philosopher.
Number the philosophers 0 to 4 and the forks 0 to 4. Philosopher i has fork i on the left and fork (i+1)%5 on the right. A good solution must satisfy three conditions:
- No two neighbours eat at the same time, because they would share a fork.
- No deadlock.
- No starvation. Every hungry philosopher eventually eats.
The naive solution and its deadlock
Give each fork a binary semaphore initialised to 1.
semaphore fork[5] = {1, 1, 1, 1, 1};
void philosopher(int i) {
while (1) {
think();
wait(fork[i]);
wait(fork[(i + 1) % 5]);
eat();
signal(fork[i]);
signal(fork[(i + 1) % 5]);
}
}Mutual exclusion on each fork holds, so neighbours never eat together. The program can still deadlock. Suppose a context switch happens right after each philosopher executes the first wait. Every philosopher then holds the left fork and waits for the right fork, which a neighbour holds. Nobody can proceed.
This state meets all four deadlock conditions:
- Mutual exclusion: a fork has one holder at a time.
- Hold and wait: each philosopher holds one fork while requesting another.
- No preemption: a fork cannot be taken away from its holder.
- Circular wait: P0 waits for P1, P1 for P2, and so on back to P0.
Every fix below removes at least one of these conditions. Mutual exclusion cannot be removed, because a fork really is exclusive.
Fix 1: limit the number of diners
Add a counting semaphore room initialised to 4. A philosopher must wait(room) before picking up forks and signal(room) after putting them down.
semaphore fork[5] = {1, 1, 1, 1, 1};
semaphore room = 4;
void philosopher(int i) {
while (1) {
think();
wait(room);
wait(fork[i]);
wait(fork[(i + 1) % 5]);
eat();
signal(fork[(i + 1) % 5]);
signal(fork[i]);
signal(room);
}
}At most four philosophers compete for five forks. By the pigeonhole principle, at least one of the four can get both of its forks. That philosopher eats and then releases both forks, so the others can continue. A circular wait among all five cannot form, because only four are at the table.
The value 4 is the largest that is safe. With room = 5 the naive deadlock returns. With room = 4 the system is deadlock-free, and a semaphore queue served in FIFO order also prevents starvation. In general, with n philosophers and n forks, room = n - 1 is safe.
Fix 2: break the symmetry
The naive solution deadlocks because every philosopher acts identically. An asymmetric rule breaks the cycle. Even-numbered philosophers pick up the left fork first, and odd-numbered ones pick up the right fork first.
semaphore fork[5] = {1, 1, 1, 1, 1};
void philosopher(int i) {
int first = (i % 2 == 0) ? i : (i + 1) % 5;
int second = (i % 2 == 0) ? (i + 1) % 5 : i;
while (1) {
think();
wait(fork[first]);
wait(fork[second]);
eat();
signal(fork[second]);
signal(fork[first]);
}
}Two neighbours now compete for the same fork as their first choice, so at most one of them holds a fork while waiting for the other. That removes the circular wait.
A close relative is resource ordering. Number the forks and require every philosopher to take the lower-numbered fork first. Philosopher 4 uses forks 4 and 0, so it takes fork 0 first. This is the same as the asymmetric rule for the last philosopher. Any global ordering of resources prevents circular wait, because a cycle would need some process to request a lower-numbered resource while holding a higher-numbered one.
Fix 3: pick up both forks or none
This solution removes hold and wait. A philosopher eats only when both neighbours are not eating, and it takes both forks in one step. A shared array records each state, and a mutex protects it.
#define N 5
#define LEFT(i) (((i) + N - 1) % N)
#define RIGHT(i) (((i) + 1) % N)
enum { THINKING, HUNGRY, EATING };
int state[N];
semaphore mutex = 1;
semaphore s[N] = {0, 0, 0, 0, 0};
void test(int i) {
if (state[i] == HUNGRY &&
state[LEFT(i)] != EATING &&
state[RIGHT(i)] != EATING) {
state[i] = EATING;
signal(s[i]);
}
}
void take_forks(int i) {
wait(mutex);
state[i] = HUNGRY;
test(i);
signal(mutex);
wait(s[i]);
}
void put_forks(int i) {
wait(mutex);
state[i] = THINKING;
test(LEFT(i));
test(RIGHT(i));
signal(mutex);
}Each philosopher has a private semaphore s[i] that starts at 0. If test(i) succeeds, it signals s[i] and the later wait(s[i]) in take_forks passes straight through. If it fails, the philosopher blocks on s[i] until a neighbour finishes and calls test on it. The mutex is released before the philosopher blocks. If it were held while waiting on s[i], nobody could ever call put_forks.
This solution is deadlock-free, but it can starve a philosopher. If philosophers 1 and 3 keep alternating their meals so that one of them is always eating, philosopher 2 never sees both neighbours idle and waits forever.
Comparing the solutions
| Solution | Condition removed | Deadlock-free | Starvation-free | Extra cost |
|---|---|---|---|---|
| Naive, left then right | none | No | No | none |
| `room = 4` | circular wait (by limiting contenders) | Yes | Yes with FIFO queues | one counting semaphore |
| Asymmetric or ordered forks | circular wait | Yes | Not guaranteed by the pattern alone | none |
| Both forks or none (state array) | hold and wait | Yes | No | mutex and per-philosopher semaphores |
Common mistakes in exam questions
- Releasing forks in a different order from acquiring them does not matter for deadlock. Only the order of
waitcalls matters. - Making all philosophers pick up the right fork first is the same as the naive solution mirrored. It still deadlocks.
- Adding a timeout and putting the fork back can avoid a permanent deadlock. All five philosophers can still keep retrying in lockstep, which is livelock.
- Maximum concurrency is two philosophers eating at once with five forks. Each eater uses two forks, and
2 × 2 = 4 ≤ 5, but three eaters would need six.
Exercise
In the naive solution with five philosophers, each acquiring the left fork then the right fork, a counting semaphore room is added as in Fix 1. What is the largest initial value of room that guarantees freedom from deadlock, and why?
Show answer
The largest value is 4. If at most four philosophers are at the table, five forks cannot all be held in a circular wait, and at least one philosopher can obtain two forks. With room = 5, all five can each pick up their left fork and block on the right one, which is the deadlock.
Exercise
Philosophers 0 to 3 pick up the left fork first, and philosopher 4 picks up the right fork first (fork 0, then fork 4). Is deadlock possible? Name the deadlock condition this change removes.
Show answer
Deadlock is not possible. This change removes circular wait. For a cycle to form, philosopher 4 would hold fork 4 and wait for fork 0, while P0 holds fork 0 and waits for fork 1, and so on. But P4 requests fork 0 first and does not hold fork 4 while waiting for it. The chain of waits therefore ends and cannot loop back.