For the prelab assignment, you are to write a Java program that simulates the unconstrained version of this problem - i.e., a situation in which the philosophers use no utensils: kind of like a pie-eating contest. You need to write two classes:
Write the Java code for this problem, and run it briefly to test it. Hand in your code and a test run.
The Dining Philosophers problem in its standard form is a resource allocation problem. As in the version you implemented in the prelab, it consists of five philosophers sitting around a table, thinking and eating. However, to make it more interesting, our philosophers are going to be eating Chinese food. They will do this with chopsticks. Being philosophers, they are not nearly as well-paid as computer scientists, and so can only afford five chopsticks. One of these chopsticks is placed between each pair of philosophers around the table. To eat, a philosopher must have two chopsticks: the one to his or her left, and the one to his or her right. Note that this means that it is not possible for two neighboring philosophers to eat at the same time.
You should implement a solution using your Semaphore implementation from last lab. In particular, implement each chopstick as a mutual exclusion semaphore; the Philosopher thread that successfully passes through any such semaphore is assumed to have picked up the corresponding chopstick. A Philosopher must pick up both chopsticks in order to be able to eat. This means that you can think of the ``eating'' section (printing the message indicating that eating has begun, the call to napping() that simulates the eating time, and the message indicating that eating is finished) as the critical section of the Philosopher code.
Your implementation should follow the following guidelines, which are consistent with the solution in the text:
Compile and run the resulting program. You should observe that no two consecutive philosophers are eating at the same time. Hand in your revised code and a test run of this program, and your answers to the following questions: Do you observe any problems when your program runs? Based on the code, what problems could occur?
Note: Once you can cause deadlock to happen, it's a good idea to run the program a number of times to see what happens. With a good choice of when to nap and napping time, you'll see that sometimes your program deadlocks, and sometimes it will run for a long time without deadlocking. This shows that deadlock is the result of a particular series of events; having the potential for deadlock in your code is not a guarantee that it will occur. This can make debugging a real pain.
Hand in your revised code and a test run of this program showing an occurrence of deadlock. What situation do you observe occurring that leads to the deadlock?
Problems with symmetrical behavior are common in computer science. It is a standard issue that one needs to think about in dealing with parallel algorithms where multiple processes or threads act on a set of common resources, for example.
How do we break symmetry? In parallel algorithms, a common technique is to apply randomness at the point of symmetry. For example, separate processes can each flip a coin (or the computational equivalent), then decide what to do based on the result. We could do this in the Dining Philosophers solution, and greatly decrease the probability of having deadlock occur. (In this case, each philosopher would flip a coin to decide which chopstick to pick up first. Unfortunately, the use of randomness would still allow the possibility of choosing symmetric behavior, though with a small probability. When we are dealing with making choices to help a parallel algorithm proceed a little faster, this isn't a problem. But with the possibility of deadlock, we need to be more careful.
One way to break symmetry in the Dining Philosophers is to let different Philosopher objects behave differently. There are a couple of obvious possibilities:
Hand in your revised code for both of these solutions and a test run of each. Also answer the following questions: Do these solutions solve all the potential problems? If not, what problem can still occur?
The DiningPhilosophers class provides the synchronization and protection for the philosophers and their chopsticks via the pickUp and putDown methods. The test method is a key element.
In the description on page 245 of the text, both the pickUp and test involves a notion of self. Think carefully how you would implement this. How to keep a thread itself in a wait state if the philosopher is not in the eating state? And the signal in the test method is really a notification, not the release that we have in the operation of a semaphore.
This will give each philosopher access to the pickUp and putDown methods. Modify the run method appropriately.
Hand in the code for the three classes along with output from a sample run. Give a brief explanation showing that this solution is deadlock free.
This document was generated using the LaTeX2HTML translator Version 2002-2-1 (1.70)
Copyright © 1993, 1994, 1995, 1996,
Nikos Drakos,
Computer Based Learning Unit, University of Leeds.
Copyright © 1997, 1998, 1999,
Ross Moore,
Mathematics Department, Macquarie University, Sydney.
The command line arguments were:
latex2html -nonavigation -split 1 lab
The translation was initiated by CS315 on 2004-03-10