Dining Philosophers

The Dining Philosophers problem illustrates the following scenario:

Five philosophers are seated at a table with a large bowl of food in the middle. Between each pair of philosophers is one chopstick, and to eat a philosopher must use both chopsticks beside him. Each philosopher spends his life in the following cycle: He thinks for a while, gets hungry, picks up one of the chopsticks beside him, then the other, eats for a while and puts the chopsticks down on the table again. If a philosopher tries to grab a chopstick but it is already being used by another philosopher, then the philosopher waits until that chopstick becomes available. This implies that no neighboring philosophers can eat at the same time and at most two philosophers can eat at a time.

This model executes until deadlock occurs (or until you push the stop button). Deadlock occurs when each of the philosophers acquires one chopstick. No special measures are taken in this model to avoid deadlock, so it will eventually occur.

The Dining Philosophers problem was first described by Edsger W. Dijkstra in 1965. It is a classic concurrent programming problem that illustrates the two basic properties of concurrent programming:

  • Liveness. How can we design the program to avoid deadlock, where none of the philosophers can make progress because each is waiting for someone else to do something?
  • Fairness. How can we design the program to avoid starvation, where one of the philosophers could make progress but does not because others always go first?
  • The above applet uses an algorithm that lets each philosopher randomly chose which chopstick to pick up first, and all philosophers eat and think at the same rates. This algorithm is fair as any time a chopstick is not being used and both philosophers try to use it, they both have an equal chance of succeeding. However this algorithm does not guarantee the absence of deadlock, and if it is let run long enough this will eventually occur. The probability that deadlock occurs sooner increases as the thinking times are decreased relative to the eating times.

    The applet is implemented using the Communicating Sequential Processes (CSP) domain in Ptolemy II. The CSP domain models a system as a network of process communicating with messages through unidirectional channels. The communication is rendezvous based, as both the sending and receiving processes block until the other side is ready to communicate. The model of communication used in the CSP domain is different from the CSP model normally encountered in that a notion of time has been added to the model, although this model of time is not used in this applet.

    In the above applet, each chopstick and each philosopher is represented as a separate process. A chopstick must be ready to communicate with either philosopher beside it at any stage. The chopstick process uses a conditional communication construct to be ready to rendezvous at any stage. Each philosopher uses the notion of time built into the CSP domain to allow itself to delay while it is eating or thinking. It should be noted that the applet has been slowed down to allow the progression of the problem to be seen.