4010-441 Principles of Concurrent System Design
The Dining Philosophers

This page is accessible at www.se.rit.edu/~se441/Assignments/Dining_Philosophers.html.

Overview

For this activity your team will implement the Dining Philosopher's problem. You'll be able to test it under conditions leading to deadlock as well as those that prevent deadlock.

Classes

Fork Class

Create a passive (non-threaded) Fork class; below is the interface to be implemented by Fork.
 
public interface IFork {
    /*
     * A philosopher (attempts to) acquire the fork.
     */
    public void acquire() ;

    /*
     * A philosopher releases the fork.
     */
    public void release() ;
}
There is only a default Fork constructor. Note that a Philosopher acquiring the Fork may have to wait if the Fork is already allocated; when the Fork is released, any waiting Philosopher must be notified. Add state as necessary to the Fork class to support these operations; the initial state is unallocated.

Philosopher Class

The Philosopher class extends Thread:

public Philosopher(int id, Fork left, Fork right, boolean rHanded,
                   int nTimes, long thinkMillis, long eatMillis)
Initialize the new Philosopher  thread object. The idleft and right arguments are self-explanatory, rHanded is true if the Philosopher is right handed and false if left handed, and nTimes represents the number of think / eat cycles before the Philosopher exits (0 means run forever). The thinkMillis and eatMillis represent the maximum think time and eating time, respectively, for each cycle.
public void run()
Enter a loop executed nTimes (or infinitely if nTimes is 0). On each iteration:

Driver Class

The driver class contains the public static void main( ... ) method; your program will be executed with a command of the form:

java Driver [-l] [np] [nt] [tm] [em]

-l is a literal "minus ell", and np, nt, tm and em are non-negative integers.

The driver first creates an array of np Forks. Next, np Philosopher threads are created, where Philosopher i's right fork is i and left fork is
(np + i - 1) % np - note that i ranges from 0 to np - 1.

Execution

You should execute your program at least four times with different parameters, capture the output, and create one long file Trace.txt from the results. The different traces in Trace.txt should be separated from each other so as to make them easy to distinguish. Add comments to the ends of lines in the trace that illustrate interesting states of the system.

In particular, you should have at least one trace with all right handed philosophers that results in a deadlock. I'd suggest running the program as follows (though other combinations may work better for you):

java Driver 2 0 0 0

You need only show the output from the first fork acquisition leading to the deadlock occurs to the deadlock itself.

Deliverables

Submit a zip file (and only a zip file) named philosophers.zip with five files (and only five files), Driver.java, IFork.java, Fork.java, Philosopher.java, and Trace.txt to the Dining Philosophers Activity dropbox by due date. Of the 10 points available for this activity,

In all cases, points assigned represent your instructor's judgement as a professional engineer, and are not subject to negotiation.