Sub topic 14
Dining Philosophers
Assume you know process and thread basics, semaphore wait and signal operations, and how preemption can interrupt a thread between two instructions.
The dining-philosophers problem models five threads competing for five shared resources. Each philosopher alternates between thinking and eating. To eat, a philosopher needs both the chopstick on the left and the chopstick on the right. Each chopstick can be held by only one philosopher at a time.
The deadlock
A simple solution gives every philosopher a semaphore for the left chopstick and then the right chopstick:
wait(left)
wait(right)
eat()
signal(right)
signal(left)Suppose every philosopher runs wait(left) before any philosopher runs wait(right). Each philosopher now holds one chopstick and waits for the next chopstick. No philosopher can continue, so the system is deadlocked.
This execution satisfies the four deadlock conditions:
- Mutual exclusion: one philosopher owns each chopstick.
- Hold and wait: each philosopher holds one chopstick while waiting for another.
- No preemption: a chopstick is released only by its owner.
- Circular wait: each philosopher waits for a chopstick held by the next philosopher.
Ways to prevent deadlock
A solution needs a synchronization rule that breaks at least one deadlock condition.
| Strategy | Rule | Main trade-off |
|---|---|---|
| Waiter semaphore | Allow at most four philosophers to pick up chopsticks | Prevents deadlock but may limit parallelism |
| Resource ordering | Always acquire the lower-numbered chopstick first | Prevents circular wait |
| Asymmetric order | One philosopher picks up right first; others pick up left first | Simple, but less uniform |
| Try-lock and release | If the second chopstick is unavailable, release the first | Avoids deadlock but can cause starvation |
The waiter solution uses a counting semaphore initialized to four. A philosopher must enter the dining room before taking either chopstick. Since fewer than five philosophers can hold one chopstick at the same time, at least one seated philosopher can obtain both chopsticks and finish.
import threading
import time
N = 5
chopsticks = [threading.Lock() for _ in range(N)]
waiter = threading.Semaphore(N - 1)
def philosopher(i):
left = chopsticks[i]
right = chopsticks[(i + 1) % N]
for meal in range(3):
time.sleep(0.01)
waiter.acquire()
left.acquire()
right.acquire()
print(f"philosopher {i} eats meal {meal}")
time.sleep(0.01)
right.release()
left.release()
waiter.release()
threads = [threading.Thread(target=philosopher, args=(i,)) for i in range(N)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()The waiter prevents deadlock, but it does not automatically guarantee fairness. A philosopher that repeatedly loses the race for the waiter or chopsticks could starve. A fair queue, or a monitor that records waiting philosophers, can provide stronger fairness.
Exercise
Five philosophers use the waiter solution with a semaphore initialized to 4. Can all five philosophers hold one chopstick and wait forever? Explain.
Show answer
No. At most four philosophers enter. Therefore, at least one philosopher is outside and one seated philosopher can obtain both chopsticks, eat, and release its resources.