Deadlock Demolotion


Traffic Jam

Dining philosophers

A good example of deadlock is the dining philosophers problem. In this problem, there are n philosophers trying to have dinner with n chopsticks. Each requires two chopsticks to eat. How can we allocate the chopsticks such that every philosopher gets to eat?

Deadlock Dining

Who’s a good dining philosopher?

Dog Philosopher

What is deadlock?

From wikipedia:

In an operating system, a deadlock occurs when a process or thread enters a waiting state because a requested system resource is held by another waiting process, which in turn is waiting for another resource held by another waiting process.

Resource allocation graph

We can model resource allocation by having resources and processes represented as vertices and use edges to show ownership of a resource. A cycle in the (undirected) resource allocation graph implies that we have deadlock.

Example RAG

Deadlock RAG

Solutions to the dining philsophers problem


Have one authority (e.g. a mutex). Have each philosopher grab that authority and only when they have the authority can they pick up their forks and eat. They eat, put the arbitrator and the forks down and move on to the next philosopher (can be random or sequential).


  • Very slow
  • Only one thread running at a time
  • Python’s global interpreter lock is implemented this way

Leave the table (Stallings)

Consider the case of the dining philosophers with n-chopsticks and n-philosophers. Reduce the number of philosophers currently allowed at the table to n-1 using a semaphore. Have them eat. Cycle out the philosophers.


  • Very heavy on context switches for a process
  • Needs way of “pausing” a philosopher (SIGSTOP for linux kernel)
  • Need a “fair” cycling algorithm

Partial Ordering (Dijkstra)

Order the chopsticks 1..n. For each philosopher have them pick up the lower number chopstick. Then, only if they can pick up the lower chopstick, pick up the higher chopstick. Why does this work?


  • Needs to be able to order the resources
  • Doesn’t livelock but often leads one thread working at a time for large applications (databases)
  • But good for dining philosophers!

Clean/Dirty (Chandra/Misra)

If you want reeealllllllly fast (given a lot of philosophers numbered 1..n).

Chopsticks can be dirty or clean. Initially all chopsticks start out as dirty. For each pair of philosophers, assign the chopstick between them to the philosopher with the lower id. When a philosopher wants to eat, they ask the person next to them for a chopstick. If the neighbor’s chopstick is clean (they haven’t eaten yet), they keep the chopstick. Otherwise, they clean it, and give it to the requesting philosopher. This prevents starvation of philosophers and ensures priority is given to the philosopher that has least recently eaten.

A good overview of solutions

Read the Ron Swanson version here