| Dining Philosophers Problem |
Article Index for Dining |
Shopping Philosophers |
Website Links For Dining |
Information AboutDining Philosophers Problem |
| CATEGORIES ABOUT DINING PHILOSOPHERS PROBLEM | |
| concurrency | |
|
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 philosophers problem. THE PROBLEM The dining philosophers problem is summarized as five philosophers sitting at a table doing one of two things - eating or thinking. While eating, they are not thinking, and while thinking, they are not eating. The five philosophers sit at a circular table with a large bowl of spaghetti in the center. A fork is placed in between each philosopher, and as such, each philosopher has one fork to his or her left and one fork to his or her right. As spaghetti is difficult to serve and eat with a single fork, it must be assumed that in order for a philosopher to eat, the philosopher must have two forks. In the case of the dining philosopher, the philosopher can only use the fork on his or her immediate left or right. In some cases, the dining philosophers problem is explained using rice and chopsticks as opposed to spaghetti and forks, as it is generally easier to understand that two chopsticks are required, whereas one could arguably eat spaghetti using a single fork, or using a fork and a spoon. In either case, only one instrument (fork or chopstick) can be picked up at a time, and the philosopher must have two instruments in order to eat. 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, this 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 shared 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 a program is interested in is already locked by another one, the program waits until it is unlocked. When several programs 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. SOLUTIONS Not Dijkstra's Solution This is not the solution proposed in Dijkstra's paper cited below and might not even be Dijkstra's solution at all. Dijkstra's paper presents an optimal solution based on private Semaphores and state variables and a global Mutex , which was described in an older version of this article. One solution is to order the forks and require the philosophers to pick up the forks in increasing order, which mathematically eliminates the possibility of a deadlock. To illustrate this solution, label the philosophers P1, P2, P3, P4, and P5, and label the forks F1, F2, F3, F4, and F5. Each philosopher must pick up forks in a prescribed order and cannot pick up a fork another philosopher already has. Upon acquiring two forks, a philosopher may eat. Philosophers P1 through P4 follow the rule that Px must pick up fork Fx first and then may pick up fork Fx+1. For example, P1 must pick up F1 first and F2 second. Philosopher P5 must, conversely, pick up fork F1 before picking up fork F5, to respect the deadlock-preventing fork ordering rule. Although avoiding a deadlock, this solution is inefficient, because one can arrive to a situation where only one philosopher is eating and everybody else is waiting for him. For example philosophers P1 to P3 could hold forks F1 to F3, waiting to get forks F2 to F4 respectively, philosopher P5 could wait on fork F1 having no fork yet, while philosopher P4 would be eating holding forks F4 and F5. Optimally, either philosopher P1 or philosopher P2 should be able to eat in such circumstances. Preventing starvation depends on the method of Mutual Exclusion enforcement used. Implementations using Spinlock s 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. 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. Mani 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. Note this is not a solution to the original Dining Philosophers Problem because it requires that the philosophers communicate. #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), he must obtain the forks from his contending neighbours. For all such forks he does not have, he sends a request message. #When a philosopher with a fork receives a request message, he keeps the fork if it is clean, but gives it up when it is dirty. If he sends the fork over, he cleans the fork before doing so. #After a philosopher is done eating, all his forks become dirty. If another philosopher had previously requested one of the forks, he cleans the fork and sends it. This solution also allows for a large degree of concurrency, and will solve an arbitrarily large problem, assuming that any thread only needs one resource at a time. REFERENCES
SEE ALSO
EXTERNAL LINKS
|
|
|