Lably
Skip to content

Dining philosophers problem

Problem used to illustrate synchronization issues and techniques for resolving them

  • 23 min read
  • 2 chapters

Related on Lably

Through Computer science, Philosophy and Mutual exclusion

It was originally formulated in 1965 by Edsger Dijkstra as a student exam exercise, presented in terms of computers competing for access to tape drive peripherals and then formulated in terms of diners, dubbed the "dining quintet". Soon after, Tony Hoare gave the problem its present name.

Problem statement

Five philosophers dine together at the same table. Each philosopher has their own plate at the table. There is a fork between each pair of adjacent plates. The dish served is a kind of spaghetti that must be eaten with two forks. Each philosopher can only alternately think and eat. Moreover, a philosopher can only eat their spaghetti when they have both a left and a right fork. Thus, two forks will only be available when their two nearest neighbors are thinking, not eating. After an individual philosopher finishes eating, they will put down both forks. The problem is how to design a regimen (a concurrent algorithm) such that any philosopher will not starve; i.e., each can forever continue to alternate between eating and thinking, assuming that no philosopher can know when others may want to eat or think (an issue of incomplete information).

Problems

The problem was designed to illustrate the challenges of avoiding deadlock, a system state in which no progress is possible. To see that a proper solution to this problem is not obvious, consider a proposal in which each philosopher is instructed to behave as follows:

  • think unless the left fork is available; when it is, pick it up;
  • think unless the right fork is available; when it is, pick it up;
  • when both forks are held, eat for a fixed amount of time;
  • put the left fork down;
  • put the right fork down;
  • repeat from the beginning.

With these instructions, the situation may arise where each philosopher holds the fork to their left; in that situation, they will all be stuck forever, waiting for the other fork to be available: it is a deadlock.

Resource starvation, mutual exclusion, and livelock are other types of sequence and access problems.

Sign in to keep reading

One more chapter is free with a Lably account.

More in Computing and communication