What is the dining philosophers problem?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Operating Systems basics — a prerequisite for any developer role.
Answer
The Dining Philosophers problem (Dijkstra, 1965) illustrates deadlock and resource contention. Five philosophers sit at a circular table with five forks, one between each pair. A philosopher needs BOTH adjacent forks to eat. Philosophers cycle between thinking and eating. Naive deadlock scenario: all philosophers simultaneously pick up their LEFT fork → all waiting for the RIGHT fork → circular wait → deadlock! Solutions: (1) Allow only 4 philosophers to eat at once: use a semaphore initialized to 4 — at most 4 can grab forks simultaneously, guaranteeing at least one can get both: Semaphore table = new Semaphore(4); philosopher i: wait(table); wait(fork[i]); wait(fork[(i+1)%5]); EAT(); signal(fork[i]); signal(fork[(i+1)%5]); signal(table);; (2) Asymmetric solution — odd/even pick-up order: odd-numbered philosophers pick up left then right; even-numbered pick up right then left. Breaks circular wait: if (i % 2 == 0) { pick left, then right } else { pick right, then left }; (3) Resource hierarchy solution: number all forks; always pick lower-numbered fork first — breaks circular wait; (4) Chandy-Misra solution (message passing): philosophers ask neighbors to pass forks — no shared memory needed. Why it matters: illustrates how seemingly correct solutions can deadlock; teaches resource ordering, hold-and-wait elimination, and preemption as deadlock prevention strategies.
Pro Tip
This topic has Operating Systems-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.