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 id, left
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:
- Select a random integer t
from 0
through thinkMillis.
- Print
a message Philosopher id thinks for
t time units,
and sleep the selected time.
- If right handed, print Philosopher id goes for
right fork, acquire the right
fork, and when this returns print
Philosopher id has right
fork.
- Yield
to another thread using Thread.yield(
).
- Print Philosopher id goes for
left fork, acquire the left fork, and when
this returns print
Philosopher id has left
fork.
- Yield to another thread using Thread.yield(
).
- If left handed, print Philosopher id goes for
right fork, acquire the right
fork, and when this returns print
Philosopher id has right
fork.
- Select a random integer t
from 0
through eatMillis.
- Print
a message Philosopher id eats for
t time units,
and sleep the selected time.
- Release the right fork and print Philosopher
id
releases right fork.
- Release the left fork and print Philosopher
id
releases left fork.
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.
- By default, all philosophers are right handed. If -l is given, even
numbered philosophers are right handed and odd numbered philosophers
are left handed.
- np
is the number of philosophers (and forks); if omitted the default is 4.
- nt
is the number of the think / eat cycles; the default is 10.
- tm
is the think time in milliseconds; the default is 0.
- em
is the eat time in milliseconds; the default is 0.
- Command line arguments will be provided in the order shown above. Trailing arguments that are not specified will receive the default value.
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,
- 2 will be granted on an all-or-nothing basis for a correct submission in accordance with the
preceding specification,,
- 3
will be based on the correctness of the solution,
- 2 will be based on
the quality and comprehensiveness of the trace,
- 2 will be based on
the quality of the solution (naming, spacing, indentation, etc.), and
- 1 will be based on whether or not the internal documentation is of acceptable quality.
In all cases, points assigned represent your instructor's judgement as
a professional engineer, and are not subject to negotiation.