Dining philosophers problem
From Free net encyclopedia
In computer science, the dining philosophers' problem is an illustrative example of a common computing problem in concurrency. It is a classic multi-process synchronization problem, and is included in nearly all college-level computer science curriculums.
In 1971, Edsger Dijkstra set an examination question on a synchronization problem where five computers competed for access to five shared tape drive peripherals. Soon afterwards the problem was retold by Tony Hoare as the dining philosopher's problem.
Five philosophers spend their time eating and thinking. The college where they live has a dining table with a large bowl of spaghetti in the center of the table. There are five plates at the table and five forks set between the plates.
Eating the spaghetti requires the use of two forks which the philosophers pick up one at a time. The philosophers never speak to each other which creates a dangerous possibility of deadlock in which every philosopher holds a left fork and waits perpetually for a right fork (or vice versa).
Originally used as a means of illustrating the problem of deadlock the system reaches deadlock when there is a 'cycle of ungranted requests'. In this case philosopher P1 waits for the fork grabbed by philosopher P2 who is waiting for the fork of philosopher P3 and so forth, making a circular chain.
Starvation (and the pun was intended in the original problem description) might also occur independently of deadlock if a philosopher is unable to acquire both forks due to a timing issue. For example there might be a rule that the philosophers put down a fork after waiting five minutes for the other fork to become available and wait a further five minutes before making their next attempt. This scheme eliminates the possibility of deadlock (the system can always advance to a different state) but still suffers from the problem of livelock. If all five philosophers appear in the dining room at exactly the same time and each picks up their left fork at the same time the philosophers will wait five minutes until they all put their forks down and then wait a further five minutes before they all pick them up again.
The lack of available forks is an analogy to the locking of scarce resources in real computer programming, a situation known as concurrency. Locking a resource is a common technique to ensure the resource is accessed by only one program or chunk of code at a time. When the resource the program is interested in is already locked by another one, the program waits until it is unlocked. When several resources are involved in locking resources, deadlock might happen, depending on the circumstances. For example, one program needs two files to process. When two such programs lock one file each, both programs wait for the other one to unlock the other file, which will never happen.
Contents |
Dijkstra's Solution
Image:Dining philosophers.gif
Dijkstra's solution (see the reference below on "Hierarchical ordering of sequential processes") was to let each fork be represented by a binary mutex. To avoid a deadlock situation where every philosopher picks up its left fork (by locking the left mutex) and indefinitely waits for its right fork, he suggested to have a perfect semaphore which represents the dining room. The semaphore for the room has a maximum value of four. Each philosopher, i, would then:
1. Think
2. Wait(room_semaphore)
3. Wait(left_fork_i)
4. Wait(right_fork_i)
5. Eat spaghetti
6. Signal(left_fork_i)
7. Signal(right_fork_i)
8. Signal(room_semaphore)
9. Repeat 1.
Hence, only four philosophers would be able to enter the room simultaneously and no deadlock can occur. The perfect semaphore guarding the room ensures fairness, i.e. philosophers enter the room in a FIFO fashion.
Continuing the 'dining philosophers' motif, Carrol Morgan explained Dijkstra's solution as introducing a butler.
A randomized version of the problem was produced 1981 by Daniel J. Lehmann and Turing award winner Michael O. Rabin, which ensures starvation freedom with probability 1. The main idea behind the algorithm is that each philosopher before eating randomly chooses whether to go left/right or right/left.
Prevention of starvation is dependent on the method of mutual exclusion enforcement employed. Implementations using spinlocks or busy waiting can cause starvation through timing problems inherent in these methods. This problem is known as priority inversion. Other methods of mutual exclusion that utilize queues can prevent starvation by enforcing equal access to a fork by the adjacent philosophers.
Dijkstra's solution can be extrapolated to represent arbitrary contention between philosophers, but requires all the forks to be labeled correctly for such configurations.
Chandy / Misra Solution
In 1984, K. M. Chandy and J. Misra proposed a different solution to the Dining Philosophers problem to allow for arbitrary agents (numbered P1, ..., Pn) to contend for an arbitrary number of resources (numbered R1, ..., Rm). Unlike in Dijkstra's solution, these labelings can be arbitrary. They solved this general problem using an elegant solution:
- For every pair of philosophers contending for a resource, create a fork and give it to the philosopher with the lower ID. Each fork can either be dirty or clean. Initially, all forks are dirty.
- When a philosopher wants to use a set of resources (i.e., eat), it must obtain the forks from its contending neighbors. For all such forks it does not have, it sends a request message.
- When a philosopher with a fork receives a request message, it keeps the fork if it is clean, but gives it up when it is dirty. If it sends the fork over, it cleans the fork before doing so.
- After a philosopher is done eating, all its forks become dirty. If another philosopher had previously requested one of the forks, it cleans the fork and sends it.
This solution also allows for a large degree of concurrency.
References
- Template:Cite book
- Chandy, K.M.; Misra, J. (1984). The Drinking Philosophers Problem. ACM Transactions on Programming Languages and Systems.
- Dijkstra, E. W. (1971, June). Hierarchical ordering of sequential processes. Acta Informatica 1(2): 115-138.
- Lehmann, D. J., Rabin M. O, (1981). On the Advantages of Free Choice: A Symmetric and Fully Distributed Solution to the Dining Philosophers Problem. Principles Of Programming Languages 1981 (POPL'81), pages 133-138
See also
- Drinking philosophers problem
- Sleeping barber problem
- Spontaneous symmetry breaking
- Cigarette smokers problem
External links
- Discussion of the problem with solution code for 2 or 4 philosophers
- Discussion of various solutions 1
- Discussion of various solutions 2
- Distributed symmetric solutions
- Interactive example of the Philosophers problem (Java required)de:Philosophenproblem
es:Problema de los filósofos cenando fr:Dîner des philosophes it:Problema dei filosofi a cena pl:Problem ucztujących filozofów sk:Problém obedujúcich filozofov