Cover image for The Dining Philosophers Problem: A Classic Concurrency Challenge
Computer Science

The Dining Philosophers Problem: A Classic Concurrency Challenge

2024-March-28 · 8 min read

An exploration of Dijkstra's famous Dining Philosophers problem, its solutions, and real-world applications in concurrent programming.

Introduction

Imagine five philosophers sitting around a table, each with a plate of spaghetti and a fork between them. They alternate between thinking and eating. To eat, a philosopher needs two forks—one from their left and one from their right.

This simple scenario, introduced by Edsger Dijkstra in 1965, illustrates a fundamental problem in concurrent programming: resource contention and deadlock avoidance.

In this post, we'll explore:

  • What the Dining Philosophers problem teaches us about concurrency.
  • Common solutions (and their pitfalls).
  • Real-world applications (databases, operating systems, and more).

The Dining Philosophers Problem

The Problem: Deadlocks and Starvation

Setup

  • 5 philosophers (threads).
  • 5 forks (shared resources, e.g., locks).
  • Each philosopher:
    • Thinks (does non-critical work).
    • Picks up left fork (acquires lock #1).
    • Picks up right fork (acquires lock #2).
    • Eats (critical section).
    • Puts down both forks (releases locks).

What Goes Wrong?

If all philosophers pick up their left fork simultaneously:

  • Nobody can pick up the right fork (it's held by their neighbor).
  • Deadlock! Everyone waits forever.

Alternatively, poor scheduling might cause starvation—some philosophers never get to eat.

Solution #1: Resource Hierarchy (Ordered Locks)

Idea

Assign a global order to forks (e.g., numbered 0 to 4). Each philosopher must:

  • Pick up the lower-numbered fork first.
  • Then pick up the higher-numbered fork.
ordered-locks.c
1void philosopher(int id) {
2 int left = id;
3 int right = (id + 1) % 5;
4
5 if (left > right) swap(left, right); // Enforce order
6
7 while (1) {
8 think();
9 acquire(&forks[left]); // Grab smaller fork first
10 acquire(&forks[right]); // Then larger fork
11 eat();
12 release(&forks[right]);
13 release(&forks[left]);
14 }
15}

Why It Works

  • Breaks circular wait (no deadlock).
  • Drawback: Philosophers at the end of the order may starve.

Solution #2: Arbitrator (Centralized Lock)

Idea

Introduce a waiter (mutex) to control fork access:

  • Philosophers must ask permission before picking up forks.
arbitrator.c
1mutex table; // Global arbitrator
2
3void philosopher(int id) {
4 while (1) {
5 think();
6 lock(&table); // Ask waiter
7 acquire(&forks[left]);
8 acquire(&forks[right]);
9 unlock(&table); // Release waiter
10 eat();
11 release(&forks[right]);
12 release(&forks[left]);
13 }
14}

Pros & Cons

✔ Deadlock-free (only one philosopher grabs forks at a time). ❌ Poor parallelism (defeats the purpose of concurrency).

Solution #3: Partial Acquisition (Try-Lock)

Idea

Use non-blocking attempts (try_lock):

  • If the right fork isn't available, release the left fork and retry.
try-lock.c
1void philosopher(int id) {
2 while (1) {
3 think();
4 acquire(&forks[left]);
5 if (!try_acquire(&forks[right])) {
6 release(&forks[left]); // Avoid deadlock
7 continue;
8 }
9 eat();
10 release(&forks[right]);
11 release(&forks[left]);
12 }
13}

Trade-offs

✔ No deadlock (locks are released on failure). ❌ Livelock risk (philosophers might retry indefinitely).

Real-World Analogues

Database Transactions

  • Forks = row locks.
  • Deadlocks detected via timeouts or graph algorithms.

Operating Systems

  • Fork = I/O device or memory page.
  • Solutions resemble reader-writer locks.

Distributed Systems

  • Philosophers = nodes, forks = shared data.
  • Consensus protocols (e.g., Paxos) prevent deadlocks.

Key Takeaways

  • Deadlocks arise from circular dependencies.
  • Solutions:
    • Order resources (hierarchy).
    • Limit concurrency (arbitrator).
    • Allow rollbacks (try-lock).
  • No perfect solution—trade-offs depend on context.

Further Reading

Concurrent ProgrammingOperating SystemsAlgorithmsDeadlock Prevention
Photo of Asad Khan

About Asad Khan

CEO & AI Architect

Pioneering AI solutions with 15+ years of experience in machine learning and enterprise software architecture.