Question: I am having some issue with my code and I am doing part 2 mainly the part where each philosopher is supposed to eat around
I am having some issue with my code and I am doing part 2 mainly the part where each philosopher is supposed to eat around 100 times while no philosopher eats 0 times. I'm 99 percent completed I just need someone to fix a few bugs please and thank you. Attatched is my project and the code is below.
Expected Results. Obviously, the program should not deadlock and should run to completion. Further, there should be some degree of fairness among the threads. In other words, each philosopher should eat approximately the same number of times. For our project, we have 5 philosophers and 500 eats, so each philosopher should be able to eat about 100 times. Due to a number of thread scheduling issues, this could vary somewhat, namely each one should NOT eat exactly 100 time, but in no case should one or more philosophers starve (with 0 eats).
package matrixmultiplication; import java.util.*; import java.util.concurrent.Semaphore; import java.util.concurrent.ThreadLocalRandom; /*Program for dining philosopher problem */ @SuppressWarnings("unused") public class MyClass { /umber of philosophers static int philosophersNumber = 5;
static int total =0; //create array of philosopher static Philosopher philosophers[] = new Philosopher[philosophersNumber]; //create array of forks static Fork forks[] = new Fork[philosophersNumber]; //inner class fork static class Fork { //take one semaphore public Semaphore mutex = new Semaphore(1); //method to grab the fork synchronized void grab() { //try block try { mutex.acquire(); } //catch exception catch (Exception e) { e.printStackTrace(System.out); } } //method to release the fork void release() { mutex.release(); } boolean isFree() { return mutex.availablePermits() > 0; } } //Inner philosopher class static class Philosopher extends Thread { public int number; public Fork leftFork; public Fork rightFork; int eatCount= 0; Philosopher(int num, Fork left, Fork right) { number = num; leftFork = left; rightFork = right;
} //run method is implemented here public synchronized void run(){ System.out.println("Hi! I'm philosopher #" + number); //iterate until true while (true) { if(number!=4 && philosophers[number].leftFork.mutex.availablePermits() > 0 && philosophers[number].rightFork.mutex.availablePermits() >0) { leftFork.grab(); System.out.println("Philosopher #" + number + " grabs left fork.");
if(philosophers[number].rightFork.mutex.availablePermits() >0) { rightFork.grab(); System.out.println("Philosopher #" + number + " grabs right fork."); }else { leftFork.release(); System.out.println("Philosopher #" + number + " relinquishes left fork."); break; }
eat(); philosophers[number].eatCount+=1; System.out.println("philosopher #" + number +" total eats is now: " + philosophers[number].eatCount); leftFork.release(); System.out.println("Philosopher #" + number + " releases left fork."); rightFork.release(); System.out.println("Philosopher #" + number + " releases right fork."); } if(number==4 && philosophers[number].leftFork.mutex.availablePermits() > 0 && philosophers[number].rightFork.mutex.availablePermits() >0) { rightFork.grab(); System.out.println("Philosopher #" + number + " grabs right fork.");
if(philosophers[number].leftFork.mutex.availablePermits() >0) { leftFork.grab(); System.out.println("Philosopher #" + number + " grabs right fork."); }else { rightFork.release(); System.out.println("Philosopher #" + number + " relinquishes right fork."); break; }
eat();
leftFork.release(); System.out.println("Philosopher #" + number + " releases left fork."); rightFork.release(); System.out.println("Philosopher #" + number + " releases right fork."); } } } void eat() { try { int sleepTime = ThreadLocalRandom.current().nextInt(0, 3000); System.out.println("Philosopher #" + number + " eats for " + sleepTime + " milliseconds."); Thread.sleep(sleepTime); total++; System.out.println(total); } catch (Exception e) { e.printStackTrace(System.out); } } } public static void main(String argv[]) { System.out.println("Dining philosophers problem."); for (int i = 0; i

Programming Project #4 The Dining-Philosophers Problenm In Section71.3, we provide an outline of a solution to the dining-philosophers problem using monitors. This project involves implementing a solution to this problem using either POSIX mutex locks and condition variables or Java condition variables. You only need to implement with one of these two solutions. Solutions will be based on the algorithm illustrated in Figure z.z. Both implementations will require creating five philosophers, each identified by a number o. .4. Each philosopher will run as a separate thread. Philosophers alternate between thinking and eating. To simulate both activities, have each thread sleep for a random period between one and three seconds. The program stops when all the cats add up to 500. I. POSIX Thread creation using Pthreads is covered in Section 4.41. When a philosopher wishes to eat, she invokes the function pickup torks(int pllosopher number) where philosopher_number identifies the number of the philosopher wishing to eat. When a philosopher finishes eating, she invokes return forks(int philosopher number) Your implementation will require the use of POSIX condition variables, which are covered in Section z3 II. Java When a philosopher wishes to eat, she invokes the method takeForks (philosopher Number ), where philosopher Number Identifies the number of the philosopher wishing to eat. When a philosopher finishes eating, she invokes returnF orks (philosopherNumber) Your solution will implement the following interface public interface DiningServer /Called by a philosopher when it wishes to eat +/ public void takeForks(int philosopherNumber /* Called by a philosopher when it is finished eating / public void returnFor ks [int philosopherNumber); It will require the use of Java condition variables, which are covered in Section 7.4.4 Programming Project #4 The Dining-Philosophers Problenm In Section71.3, we provide an outline of a solution to the dining-philosophers problem using monitors. This project involves implementing a solution to this problem using either POSIX mutex locks and condition variables or Java condition variables. You only need to implement with one of these two solutions. Solutions will be based on the algorithm illustrated in Figure z.z. Both implementations will require creating five philosophers, each identified by a number o. .4. Each philosopher will run as a separate thread. Philosophers alternate between thinking and eating. To simulate both activities, have each thread sleep for a random period between one and three seconds. The program stops when all the cats add up to 500. I. POSIX Thread creation using Pthreads is covered in Section 4.41. When a philosopher wishes to eat, she invokes the function pickup torks(int pllosopher number) where philosopher_number identifies the number of the philosopher wishing to eat. When a philosopher finishes eating, she invokes return forks(int philosopher number) Your implementation will require the use of POSIX condition variables, which are covered in Section z3 II. Java When a philosopher wishes to eat, she invokes the method takeForks (philosopher Number ), where philosopher Number Identifies the number of the philosopher wishing to eat. When a philosopher finishes eating, she invokes returnF orks (philosopherNumber) Your solution will implement the following interface public interface DiningServer /Called by a philosopher when it wishes to eat +/ public void takeForks(int philosopherNumber /* Called by a philosopher when it is finished eating / public void returnFor ks [int philosopherNumber); It will require the use of Java condition variables, which are covered in Section 7.4.4
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
