Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Dining Philosophers Consider the solution below for the dining philosophers p

ID: 3571350 • Letter: 2

Question

2. Dining Philosophers Consider the solution below for the dining philosophers problem: public class Philosopher extends Thread private int id; private Semaphore chopstick private static final int NAP TIME 5 private static final int ROUNDS 3; public Philosopher (int id Semaphore chopstick) this id id this chopstick chopstick public void run for (int i 0; i ROUNDS i++) try chopstick Cid] acquire C); chopstick (Cid 1) 5) acquire C): eat catch (Interrupted Exception e) e printstackTrace C); finally f chopstick Cid] release chopstick [Cid 1) 5] release think private void eat System. out. println Philosopher id eating") int sleeptine (int) (NAP-TIME Math .random try Thread sleep (sleeptime 1000) catch (InterruptedException e) e. printStackTrace private void think System out printin Philosopher id thinking") int sleeptine (int) (NAP-TIME Math random try Thread sleep (sleeptine 1000) catch (Interrupted Exception e) f e printStackTrace

Explanation / Answer

In the above scenario of Dinning Philosphers problem there are two problems associated -

1-Deadlock Problem

2-Starvation

1-Deadlock-In the above solution suppose when there is a condition in which all the philosophers take one-one chopsticks then in this situation no philosopher can be able to take his food ( system can be in deadlock state ).

2-Starvation-Suppose that a philosopher get both the chopstick and starts eating food then when he eats foods then he again started to eats food in this scenario there is unfair judgement with other philosopher so it is a condition of starvation.

In order to provide solution from deadlock we introduced a constraint that philoshers sitting on the even positions first pick chopstick on his left side then his right side where as the philosopher sitting on the odd positions first pick chopstick on his right side then his left side.

For other questions solutions please provide as seprate questions ,Thankyou!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote