Science Fair Project Encyclopedia
Dining philosophers 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).
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.
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 similiar 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 arbitray 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 resources 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 (i.e. The agent controls resources R2, R5, R9, R15, and R20. It must release R15 and R20 before requesting access to R12).
- Discussion of the problem with solution code for 2 or 4 philosophers
- A solution to the dining philosophers problem
- Discussion of various solutions 1
- Discussion of various solutions 2
- Distributed symmetric solutions
- Interactive example of the Philosophers problem (Java required)
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details