Question: Using Java; Create a non-deadlocking version of the Dining Philosopher Problem. Your implementation shall incorporate the Arbitrator solution. Keep in mind that the critical section
Using Java;
Create a non-deadlocking version of the Dining Philosopher Problem. Your implementation shall incorporate the Arbitrator solution. Keep in mind that the critical section for a philosopher is when it checks if its forks are available and possibly picks them up. The waiter (Arbitrator) doesnt actually grant permission to pick up the forks. The waiter just ensures that only one philosopher at a time checks and picks up its forks.
Requirements:
Philosophers shall be implemented as java.lang.Runnable objects. The Runnable.run() method needs a while loop that allows a philosopher to repeatedly think and eat. The while condition is true when the philosopher is instantiated, but must be changeable from outside the class using an accessor method, public void setStopFlag(). Philosophers must not think or eat inside the critical section.
Inside the Runnable.run() method a philosopher needs to accumulate and record several data items. These data need to be saved in private instance variables and accessible from outside the class by public accessor methods. The data include:
total loop iterations
total number of times a philosopher eats
accumulated time spent waiting for the waiter or processing the critical section
total time spent in the run method
Time durations can be in milliseconds (System.currentTimeMilis() or nanoseconds (System.nanoTime()).
A Main or Driver object (its main method) should instantiate and initialize all data structures, in particular forks, philosophers, and threads. After starting each thread (Thread.start()), main should sleep for a number of milliseconds as specified on the command line. When the main thread wakes up, it should signal each thread to stop (do not interrupt) and wait for the thread to complete.
Skeleton Code:
public class Fork { private int id; private boolean availableFlag = true; private static final String pickUpMsg = " is not available"; private static final String putDownMsg = " is not being held"; public Fork(int id) { this.id = id; } public boolean isAvailable() { return availableFlag; } public void pickUp() { if (!availableFlag) { throw new IllegalStateException(toString() + pickUpMsg); } availableFlag = false; } public void putDown() { if (availableFlag) { throw new IllegalStateException(toString() + putDownMsg); } availableFlag = true; } }
public static void main(String[] args) throws IOException { Fork[] forks = new Fork[5]; for (int i = 0; i < 5; i++) { forks[i] = new Fork(i); } Object waiter = new Object(); Philosopher[] phil = new Philosopher[5]; phil[0] = new Philosopher(names[0], waiter, forks[0], forks[4]); for (int i = 1; i < 5; i++) { phil[i] = new Philosopher(names[i], waiter, forks[i], forks[i-1]); } Thread[] th = new Thread[5]; for (int i = 0; i < 5; i++) { th[i] = new Thread(phil[i]); System.out.println("Philosopher Id: " + phil[i].getId()); } for (int i = 0; i < 5; i++) { th[i].start(); } try { Thread.sleep(sleepTime); for (int i = 0; i < 5; i++) { phil[i].setStopping(true); th[i].join(); } } catch (InterruptedException ie) { ie.printStackTrace(); } public void run() { long startTotalTime = System.nanoTime(); boolean haveForks = false; while (!isStopping()) { // think thoughts++; long thinkTime = (long)(random.nextFloat() * 10.0); try { Thread.sleep(thinkTime); } catch (InterruptedException e) { e.printStackTrace(); } // try to get forks long startMutexTime = System.nanoTime(); synchronized(waiter) { if (leftFork.isAvailable() && rightFork.isAvailable()) { try { leftFork.pickUp(); rightFork.pickUp(); haveForks = true; } catch (IllegalStateException e) { String msg = id + " in illegal state. " + e.getMessage(); System.out.println(msg); setStopping(true); } } } estMutexTime += System.nanoTime() - startMutexTime; // eat? if (haveForks) { meals++; long eatTime = (long)(random.nextFloat() * 10.0); try { Thread.sleep(eatTime); } catch (InterruptedException e) { e.printStackTrace(); } try { leftFork.putDown(); rightFork.putDown(); haveForks = false; } catch (IllegalStateException e) { String msg = id + " in illegal state. " + e.getMessage(); System.out.println(msg); setStopping(true); } }
} estTotalTime = System.nanoTime() - startTotalTime; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
