Introduction

In this project, you are going to use Java threads and semaphores to implement a solution for the famous Dining Philosophers problem. A description of the problem is available in course slides. You will make use of a GUI class (provided to you) to visualize your solution. You are free to use any development environment (that supports Java, of course) and OS you like. Eclipse works on multiple platforms and is generally a good option. You can download it from http://www.eclipse.org/downloads/.

GUI and Related Functions

The partial program that we provide on SUCourse has all necessary functions to initialize and update the GUI. You do not need to and you should not change GUI-related code. If you really think something needs to be changed, let us know first.

This is what you should be getting when you download and run the code directly without making any changes: See image.

The numbers next to the plates indicate which philosopher that plate belongs to. In your implementation, your philosophers should also be numbered from 0 to 4 (NOT 1 to 5) so that the GUI works properly.

The functions that are given to you are:

Hungry_GUI(int i) : This function should be called when a philosopher is hungry. It will cause the i’th philosopher’s plate to turn yellow. It will then put the thread to sleep for 0.5 seconds. This helps us visualize that the philosopher’s state is first set to hungry and then he takes the forks and starts eating.

ForkTake_GUI(int i) : This function should be called when a philosopher takes the forks that are next to his plate. The forks next to the i’th philosopher’s plate will turn red to indicate that they are being used.

Eating_GUI(int i) : When a philosopher starts eating, call this function to turn the plate of the i’th philosopher to red. Each philosopher eats for three seconds when this function is called.

StopEating_GUI(int i) : This function should be called when a philosopher stops eating. It will cause the plate of the i’th philosopher to go back to black&white.

ForkPut_GUI(int i) : After a philosopher is done eating, he puts the forks back on the table. When this function is called, the forks next to the i’th philosopher’s plate will become black&white.

Task & Implementation Details

Here’s a short description of the problem: We have five forks and five plates on a table, and our five philosophers are sitting around the table. These philosophers either think or eat. In order to eat, they need two forks, the one on their left and the one on their right. If the forks are available, they start eating; otherwise they need to wait until both forks are available. Due to different scenarios, as discussed in class, these philosophers may be deadlocked or they might starve. We need an algorithm to avoid these situations.

You will have five Java threads, one for each philosopher. You need to implement an algorithm so that no philosopher starves and they are never deadlocked. You should make use of semaphores and mutexes in order to achieve this.

Each philosopher thinks for a random amount of time, between 0 to 10 seconds. The thinking time should vary for each philosopher at each iteration, so you need to make use of random number generators . After that, he becomes hungry and decides to eat. When a philosopher is hungry, his plate should turn yellow. If he can eat, then he starts eating; otherwise he waits in a hungry state (he doesn’t go back to thinking!). When he is eating, his plate should turn red. After he finishes eating, he will go back to thinking. While the philosopher is thinking, his plate should be black and white. This procedure goes on forever. For the duration of the program, you should also pay attention to the forks; the ones that are picked up and being used should be red, while the idle ones should stay black and white. Note that a philosopher does not pick any forks up while he is hungry, forks should only be picked up when the philosopher is allowed to eat.

We have given you a starting point, a partial implementation of the Philosopher class that currently only initializes the GUI. You may decide to continue from that point, or use a different approach. Notice that there may be multiple ways to solve the problem, but in any case you will need a main function that creates the GUI and the threads.

DO NOT:

  • try to use some built-in type as your semaphores. (such as “typedef int semaphore”) Java has its own semaphore class, and its documentation is available on the Web. Make use of Java’s semaphore class.
  • implement a partially working or wrong solution. (such as the case in which only one philosopher can eat, but there are enough forks on the table for two)
  • totally forget about the GUI and use other means of output (for example System.out.println(“..”) to print on the console). While this approach might be handy if you want to debug your program, we will not consider these sorts of output when we grade your program. What happens in the background should be accurately conveyed on the GUI.

Here is a screenshot of an instance of what the final product should look like: See image.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.