# Readers-writers and dining philosophers problems

> Explain the readers-writers problem and the dining philosophers problem, and how each can be solved without deadlock or starvation.

Canonical: https://www.internhack.in/learn/interview/operating-systems-interview/os-readers-writers-and-dining-philosophers
Last updated: 2026-11-28

Difficulty: Advanced

## Question

Explain the readers-writers problem and the dining philosophers problem, and how each can be solved without deadlock or starvation.

## Answer

These two problems are the remaining classics of synchronization, and each teaches a different lesson. Readers-writers teaches that not all access is equal: reading is safe to share, writing is not. Dining philosophers teaches how deadlock can arise from perfectly reasonable local behaviour. Let me take them in turn.

In the readers-writers problem, a shared data set (a database, a cache, a file) is accessed by readers, who only read, and writers, who modify. Any number of readers can safely access the data simultaneously, but a writer needs exclusive access, with no readers and no other writers present. The first solution, the readers-preference version, uses a counter readcount protected by a mutex, and a semaphore wrt for exclusive writing. The first reader to arrive locks wrt on behalf of all readers, and the last reader to leave unlocks it. Writers simply wait on wrt. This maximizes concurrency but can starve writers: if readers keep arriving in an overlapping stream, readcount never drops to zero. The second solution, writers-preference, blocks new readers when a writer is waiting; it protects writers but can starve readers. The third solution, a fair one, uses a turnstile semaphore that forces everyone through a queue in order. In practice this is packaged as a read-write lock, such as pthread_rwlock_t or Java's ReentrantReadWriteLock, which is worth using when reads greatly outnumber writes.

The dining philosophers problem, from Dijkstra, has five philosophers sitting around a circular table with a bowl of noodles, and a single chopstick between each pair of neighbours, so five chopsticks in total. A philosopher alternates between thinking and eating, and to eat needs both the left and right chopsticks. It models processes competing for multiple shared resources.

The naive solution, each philosopher picks up the left chopstick then the right, fails badly. If all five pick up their left chopstick simultaneously, each waits forever for the right one held by a neighbour: a circular wait, and hence deadlock. Even fixing deadlock can leave starvation, where one philosopher is unlucky forever.

There are several standard fixes, and each attacks a different Coffman condition. Limit the number of philosophers at the table to four using a counting semaphore, so at least one can always get two chopsticks; this prevents circular wait. Impose a resource ordering: number the chopsticks and always pick up the lower-numbered one first, so the last philosopher picks right before left; this breaks the cycle. Use an asymmetric strategy where odd philosophers pick left first and even philosophers pick right first. Or pick up both chopsticks atomically inside a critical section, or use a monitor with a state for each philosopher (thinking, hungry, eating) so that a philosopher may eat only if neither neighbour is eating. The monitor solution is deadlock-free, but a philosopher can still starve if the two neighbours alternate eating in an unlucky rhythm.

The most important idea to carry away is that resource ordering is the simplest and most general deadlock prevention technique in real code, for example always acquiring locks in a global order. The problem also connects to database transactions, where locking multiple rows in inconsistent order causes deadlocks.

When asked in an interview, do not merely recite the code. Identify the shared resources, the failure mode (deadlock or starvation), and which condition your fix removes.

## Code examples

### Dining philosophers with ordered locks (Python)

```
import threading, time, random

N = 5
forks = [threading.Lock() for _ in range(N)]

def philosopher(i):
    left, right = i, (i + 1) % N
    first, second = min(left, right), max(left, right)   # global order
    for _ in range(3):
        time.sleep(random.random() * 0.01)                # think
        with forks[first]:
            with forks[second]:
                print(f"philosopher {i} eating")
                time.sleep(0.01)

ts = [threading.Thread(target=philosopher, args=(i,)) for i in range(N)]
for t in ts: t.start()
for t in ts: t.join()
print("done - no deadlock")
```

Always taking the lower-numbered fork first breaks the circular wait, so the program cannot deadlock.

## Key points

- Readers-preference can starve writers; writers-preference can starve readers.
- Read-write locks are the practical form of the readers-writers solution.
- Naive dining philosophers deadlocks when everyone holds the left chopstick.
- Fixes: limit to N-1 diners, order resources, asymmetric pickup, atomic pickup.
- Global lock ordering is the most reusable deadlock-prevention technique.

## Common follow-up questions

- Which Coffman condition does resource ordering break?
- How would you design a fair readers-writers lock?
- Can the monitor solution to dining philosophers still starve someone?
- When is a read-write lock slower than a plain mutex?

## Interview tips

- Say which failure (deadlock versus starvation) each variant risks.
- Map each dining fix to the Coffman condition it removes.
- Relate the problem to database lock ordering to show practical understanding.

## Concepts covered

readers-writers, dining philosophers, starvation, deadlock avoidance, synchronization

Previous: [The producer-consumer (bounded buffer) problem](https://www.internhack.in/learn/interview/operating-systems-interview/os-producer-consumer-problem) · Next: [Spinlocks, test-and-set and when to spin versus sleep](https://www.internhack.in/learn/interview/operating-systems-interview/os-spinlocks-and-atomic-instructions) · [All of Operating Systems](https://www.internhack.in/learn/interview/operating-systems-interview)

## More Operating Systems questions

- [Deadlock: four necessary conditions and prevention strategies](https://www.internhack.in/learn/interview/operating-systems-interview/os-deadlock-conditions-and-prevention)
- [Banker's algorithm: safe state and worked example](https://www.internhack.in/learn/interview/operating-systems-interview/os-bankers-algorithm-worked-example)
- [Deadlock detection and recovery](https://www.internhack.in/learn/interview/operating-systems-interview/os-deadlock-detection-and-recovery)
- [Paging, page tables and the TLB](https://www.internhack.in/learn/interview/operating-systems-interview/os-paging-page-tables-and-tlb)
- [Segmentation vs paging and segmented paging](https://www.internhack.in/learn/interview/operating-systems-interview/os-segmentation-vs-paging)
- [Internal vs external fragmentation and how to reduce them](https://www.internhack.in/learn/interview/operating-systems-interview/os-internal-vs-external-fragmentation)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Operating Systems](https://www.internhack.in/learn/interview/operating-systems-interview)
