Programming Assignment: Distributed Dining Philosophers

This assignment is partially based on Project 2, on page 252 of the course textbook. You will need to understand thread synchronization and RMI in Java. You may want to review the semaphore based solution to the Dining Philosophers Problem in section 5.7.3 of the textbook as well as the monitor-based solution in section 5.8.2.

Provide a  solution to the dining-philosophers problem using Java's RMI. Java provides several mechanisms for synchronizing communication among the philospher threads, including monitors (via synchronized and waits and notifies), semaphores, ReentrantLocks and Conditions. (There is a great discussion of various Java thread coordination tools at http://www.javaworld.com/article/2078848/java-concurrency/java-101-the-next-generation-java-concurrency-without-the-pain-part-2.html.) Your solution should ensure that no philosopher can starve.  I.e., when a philosopher thread blocks, there should be a guaranteed finite amount of time before that philosopher will eat (i.e., the thread will be awakened). Your philosophers should "dine" and "think" enough times to demonstrate all five working concurrently (5-10 times is probably sufficient), with philosopher's occasionally having to "wait" for a neighbor to finish eating.

Your five philosopher threads must run on at least two different machines. E.g., you might have two philosopher threads running on one machine, and three on the other. The resources that are shared amongst the philosophers (chopsticks, table, etc.) should reside on one machine and access to them should be controlled by one RMI server.

What to turn in:

Submit, via Canvas, as many java files as you want, but there must be at least one class called DineMain, and one called Philosophers.  I will run your programs by compiling all the java files and then doing a java -Djava.security.policy=policy.txt DineMain on one machine (let XXX be its IP address) and then doing java -Djava.security.policy=policy.txt Philosophers 2 XXX on the same machine and then java -Djava.security.policy=policy.txt Philosophers 3 XXX on the other machine. Your submission should contain a README file that explains how your program prevents any philosophers from starving.

See my notes for running RMI-based processes.

Expected Output:

When a philosopher begins to think it should print a message something like:
Philosopher #n is thinking deep thoughts.
where n is the philosopher's unique ID, a number between 0 and 4.  When a philosopher becomes hungry it should print a message something like:
Philosopher #n is hungry, and looking for chopsticks.
When a philosopher begins to eat it should print a message something like:
Philosopher #n has started eating.
When the philosopher is done eating it should print a message something like:
Philosopher #n is stuffed!  She's placing her chopsticks back on the table.
The program output, then, will consist of a sequence of lines like those above.  Here's a sample:

Philosopher #0 is thinking deep thoughts.
Philosopher #1 is thinking deep thoughts.
Philosopher #2 is thinking deep thoughts.
Philosopher #3 is thinking deep thoughts.
Philosopher #4 is thinking deep thoughts.
Philosopher #1 is hungry, and looking for chopsticks.
Philosopher #1 has started eating.
Philosopher #3 is hungry, and looking for chopsticks.
Philosopher #3 has started eating.
Philosopher #2 is hungry, and looking for chopsticks.
Philosopher #0 is hungry, and looking for chopsticks.
Philosopher #1 is stuffed!  She's placing her chopsticks back on the table.
Philosopher #2 has started eating.
Philosopher #1 is thinking deep thoughts.
...etc...

Each of the five philosophers should eat 5 times.  When all philosophers have eaten 5 times, the program should halt. Each philosopher should remain stuff for a random amount of time between 2 and 5 seconds. When a philosopher eats, it should eat for a random amount of time between 1 and 3 seconds.

Note that the output, above, that pertains to the "remote" philosophers should also appear on the console of the remote machine (the one running DineRemote.)

Extra Credit

For up to a full grade bonus (to be applied to any programming project), provide a graphical user interface that represents the philosophers seated around the table, the 5 chopsticks. The appearance of each philosopher's representation (say, its color), should reflect the philospher's state (thinking, eating, hungry), and how many times it has eaten already. In addition, include a widget to control the speed of the simulation, plus a "pause" and "resume" button. The quality of the GUI will determine how much of the full grade extra credit you receive. The graphics should appear on the machine running DineMain.

Warning

This project has been offered in this class in previous semesters. Make sure the code you hand in is your own. Remember that submitting, as your own, code that is mostly someone else's work can result in your dismissal from this course!