Dining Philosophers in Deadlock


Purpose:

To illustrate that deadlocks might not be readily apparent in programs nor in tests and to illustrate in practice some of the (necessary and sufficient) conditions for deadlock.

Instructions:

Look at the Java program Dining.java. It is simple example of a Dining Philosophers program, prepared for the experiments below. Compile and run the program.

  1. It is not always certain that a deadlock will show itself in tests. Modify the program such that the philosophers thinks for at least a second before getting hungry (by inserting a call of pause() after mpause() before //hungry). Run the program a few times and explain the result.

  2. Modify the program such that even numbered philosophers pick up the left fork first while odd numbered philosophers still pick up the right first. Run the program and explain the result.

  3. Using the semaphore class (Semaphore.java), modify the (original) program such that only 4 philosophers can sit down at the same time. Run the program and explain the result.

  4. A way to deal with deadlock is to back out and start all over again if a deadlock seems to have occurred. This might be the case if a resource cannot be acquired within a given time. Modify the (original) program to use the strategy where a timeout is introduced. Use the Java timed wait primitive public final void wait(long timeout) throws InterruptedException and let a fork wait 1 second before the call to get returns. Let get return true or false. (A timeout has the same effect as if the waiting thread was notified).

    Although this program cannot deadlock it may go into a livelock in which no thread makes progress. Explain how (you may try to remove the call to mpause() and/or pause between thinking and hungry).