Spring 2001
Project 1
Date Assigned: January 23, 2001
Date Due: February 6th, 2001, 12:00pm
(FEEDS Date Due: February 13th, 12:00pm)
In this project we will implement the Dining Philosopher problem. The Dining Philosopher problem is a classic problem dealing with race conditions and interprocces communication. There are five philosophers sitting around a table. The philosophers like to think and then, after building up an appetite, like to eat. Unfortunately there is only five forks. One fork is placed in between each philosopher. In order for a philosopher to eat he/she must grab both the fork on the left and the fork on the right. The objective is to allow for concurrent eating while preventing against race conditions.
To implement this problem, we will be using Java version 1.2.2. This problem and its semaphore solution are described in pages 77-78 and Figure 2.13 in the textbook by Tanenbaum. The monitor solution is shown on page 204 in Silberschatz's book.
There are two aspects of this implementation. First to achieve concurrent execution of philosophers we will use the multithreading feature of Java. Every philosopher will be running as a separate thread. The second and important aspect is the synchronization of threads hence of philosophers.
There are two ways to implement threads in Java. The first one is implementing the Runnable interface and the second is to subclass the Thread class and overriding the run() method. Feel free to choose either way that is most convenient to you and suitable to the application requirements.
Synchronization in Java is achieved through the synchronized keyword. This keyword can be used for any Java object as well as for any method. The mechanism is very similar to the monitor abstraction. If an object is defined as synchronized, that object can be accessed by only one thread at a time. Similarly a Java object can have synchronized methods and hence becomes a monitor. At any time only one synchronized method can be executed by a thread, on the object. If there is any other thread that calls one of the synchronized methods on the same object simultaneously, it cannot continue and therefore is suspended.
In addition we need a mechanism to synchronize access to shared variables.
Java provides two methods for this purpose: Wait() and NotifyAll().
These are similar to the Wait and Signal operations of the
monitor approach.
Requirements
Output Format
The output generated on screen should look like the following....
A B
C D
E
======================================
T
T
T
T
T
TF
E
TF
E
TF
PF
T
TF
E
TF
PF
T
E
PF
T
TF
E
PF
T
TF
E
PF
T
TF
PF
T
:::
:::
======================================
A,B,C,D,E are the philosophers sitting at a round table in this order.
Note that A also sits next to E.
E: philosopher is Eating
T: philosopher is Thinking
TF: philosopher is attempting to Take the Forks
PF: philosopher is attempting to Put down the Forks
Submission
You need to use the turnin command to submit your files. A typical submission is as follows:
turnin -c cop5615sp01 -p proj1 <files>
cop5615sp01 identifies the class for which, you are submitting
your project. proj1 identifies the project your are submitting.
<files> are the names of all the files in your project separated
by commas.
Please note: After you turn in your files, turnin will display a message indicating your submission is accepted. Even you get this message (due to a bug in turnin) you still need to do the following to make sure that your submission is complete:
turnin -c cop5615sp01 -p proj1 -v
Please check man pages for turnin for more information.
Local Students:
You MUST use the turnin command to submit your files. A typical submission is as follows:
turnin -c cop5615sp01 -p proj1 <files>
Note: Submissions in any means except turnin will NOT be accepted!
FEEDS Students:
turnin -c cop5615sp01 -p FEEDSproj1 <files>
FEEDS students can e-mail their projects for convenience but this is
not preferred unless there is difficulty in remote login.
Resources
Tutorial on Java Threads
http://java.sun.com/docs/books/tutorial/essential/threads/