Hello everyone, I need help to implement this question into a code: Question:For
ID: 3810285 • Letter: H
Question
Hello everyone,
I need help to implement this question into a code:
Question:For the Dining-Philoshpers problem (N philosophers), suppose that a queue and a scheduler are implemented to guarantee that at most N-1 philoshpers can be runing the critical sectionm will there still be any deadlocks that could happen?
Answer: Yes, there will still be deadlocks that could happen, while there is still extra resources, there are not enough to satisfy the philosopher needs to have both a right and left fork, even with N-1 philosophers.
Code: ( I need help implementing this in a code based)
Thank you so much for the assistances.
Explanation / Answer
For this implementation, you need to first build the Dining philosophers problem, The loop will execute for philosophers and spoons. When deadlock happens then the spoons will be exchanged with the first spoon so that again it will work seamless.
Here is the spoon class which will have spoon count and number
package diningPhilosopher;
public class Spoon
{
private static int cnt = 0;
private int spoonNum = cnt++;
@Override
public String toString()
{
return "Spoon number is : " + spoonNum;
}
}
Here is Philosopher class which will handle threads for left and right spoon.
package diningPhilosopher;
import java.util.Random;
public class Philosopher extends Thread
{
private static Random rnd = new Random();
private static int cnt = 0;
private int num = cnt++;
private Spoon left;
private Spoon right;
static int waiting = 0;
public Philosopher(Spoon left, Spoon right)
{
left = left;
right = right;
start();
}
public void philThink()
{
System.out.println(this + " is thinking");
if (waiting > 0)
{
try
{
sleep(rnd.nextInt(waiting));
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
}
}
public void philEat()
{
synchronized (left)
{
System.out.println(this + " has " + this.left + " Waiting for " + this.right);
synchronized (right)
{
System.out.println(this + " eating");
}
}
}
@Override
public String toString()
{
return "Philosopher number is:" + num;
}
@Override
public void run()
{
while (true)
{
philThink();
philEat();
}
}
}
Here is the class, diningPhilosophers which will handle the deadlock.
package diningPhilosopher;
public class DiningPhilosophers
{
private static boolean isDeadLock = true;
public static void main(String[] args)
{
Philosopher[] phil = new Philosopher[10];
Philosopher.waiting = 8;
Spoon left = new Spoon(), right = new Spoon(), first = left;
int i = 0;
while (i < phil.length - 1)
{
phil[i++] = new Philosopher(left, right);
left = right;
right = new Spoon();
}
if (isDeadLock)
{
phil[i] = new Philosopher(left, first);
}
else
{
phil[i] = new Philosopher(first, left);
}
if (args.length >= 4)
{
int delay = 3;
if (delay != 0)
{
Timeout timeout = new Timeout(delay * 1000, "Timed out");
}
}
}
}
Final Timeout class:
package diningPhilosopher;
import java.util.Timer;
import java.util.TimerTask;
public class Timeout extends Timer
{
public Timeout(int delay, final String msg)
{
super(true);
schedule(new TimerTask()
{
@Override
public void run()
{
System.out.println(msg);
System.exit(0);
}
}, delay);
}
}
Output will be dining philosophers with spoon and number. Seamless execution will happen here.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.