Dining philosophers problem
|
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.
In 1965, Edsger Dijkstra used the example of a group of philosophers to solve a synchronization problem. Five philosophers are sitting around a circular table and each has a plate of spaghetti in front of him with a fork at either side (i.e. five philosophers, five plates, and five forks).
Dining_philosophers.png
Suppose that the life of a philosopher consists of periods of eating and thinking, that each philosopher needs two forks to eat, and that forks are picked up one at a time. After successfully picking up two forks, a philosopher eats for a while and then puts down the forks and thinks. The problem consists in developing an algorithm to avoid starvation and deadlock. Deadlock might occur if each of the five philosophers has one fork and no one can get a second fork. The 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 of deadlock. Starvation might also happen independently of deadlock if a philosopher is unable to acquire both forks.
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 |
Solution
Dining_philosophers.gif
One solution is to order the forks and require the philosophers to pick the forks up in increasing order. In this solution the philosophers are labeled P1, P2, P3, P4, and P5 and the forks are likewise labeled F1, F2, F3, F4, and F5. The first philosopher (P1) will pick up the first fork (F1) before he is allowed to pick up the second fork (F2). Philosophers P2 through P4 will behave in a similar fashion, picking up the fork Fx before picking up fork Fx+1. Philosopher P5 however picks up fork F1 before picking up fork F5. This change in behavior for P5 creates an asymmetry that prevents deadlock.
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. Other methods of mutual exclusion that utilize queues can prevent starvation by enforcing equal access to a fork by the adjacent philosophers.
This solution can also be generalized to allow for an arbitrary number of agents (A1...Am) needing access to an arbitrary number of resources (R1...Rn) requiring exclusive access. Under this solution all agents must abide by the following rules:
- All agents must request access to the resources in increasing order (i.e. Access to R3 must be acquired before requesting access to R4).
- If an agent has access to a resource Rx and needs access to resource Ry it must release Rx before making the request if x > y. Example: The agent controls resources R2, R5, R9, R15, and R20. It must release R15 and R20 before requesting access to R12.
References
See also
External links
- Discussion of the problem with solution code for 2 or 4 philosophers (http://laser.cs.umass.edu/verification-examples/dp_standard/dp.html)
- Discussion of various solutions 1 (http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?Dining+Philosophers+Problem)
- Discussion of various solutions 2 (http://www.cs.utk.edu/~plank/plank/classes/cs560/560/notes/Dphil/lecture.html)
- Distributed symmetric solutions (http://64.233.183.104/search?q=cache:MYHZ-zyFVdEJ:www.cs.purdue.edu/homes/clifton/cs603/Philosophers.ppt)
- Interactive example (http://www.doc.ic.ac.uk/~jnm/concurrency/classes/Diners/Diners.html) of the Philosophers problem (Java (http://jdl.sun.com/webapps/getjava/BrowserRedirect?locale=en&host=www.java.com:80) required)de:Philosophenproblem
es:Problema de los filósofos cenando pl:Problem ucztujących filozofów