Figure 2.17:
Lunch time in the Philosophy Department.
|
- Five philosophers are seated around a circular table
- A philosopher needs two forks to eat
- The life of a philosopher consists of alternate periods of eating and thinking
- Write a program for each philosopher that does what it is supposed to do and never gets stuck
- one attempt is to use a binary semaphore (think)
- before starting to acquire forks, a philosopher would do a down on mutex
- after replacing the forks, he would up on mutex
- bug: only one philosopher can be eating at any instant
- the solution presented in Fig. 2.18 uses an array, state, to keep track of whether a philosopher is eating, thinking, or hungry (trying to acquire forks)
- A philosopher may move only into eating state if neither neighbor is eating
Figure 2.18:
A solution to the dining philosophers problem.
|
- The solution is deadlock-free and allows the maximum parallelism for any number of philosophers
2004-05-25