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

Operating system - Need help asap. Thanks. 1) Take the dining philosopher C prog

ID: 3750860 • Letter: O

Question

Operating system - Need help asap. Thanks.

1) Take the dining philosopher C program given and Re-write this program using Java threads.

2) Submit a printout of the source code program and 3 different output runs

3) Rewrite the code and force a deadlock.

4) Submit a printout of the source code program and 3 different output runs

Deliverables:

Your description of how you rewrote the C program into Java Threads.

Show the output from running the program 3 times. They should be different due to thread timing differences.

A listing of the code with deadlock. Make sure you explain where the deadlock is in the code and how it works.

Please do show all code and work performed.

Explanation / Answer

An initial Solution:- An initial solution would be to make each of the philosophers follow the following protocol:

while(true) {

    // Initially, thinking about life, universe, and everything

    think();

    // Take a break from thinking, hungry now

    pick_up_left_fork();

    pick_up_right_fork();

    eat();

    put_down_right_fork();

    put_down_left_fork();

    // Not hungry anymore. Back to thinking!

}

As the above pseudo code describes, each philosopher is initially thinking. After a certain amount of time, the philosopher gets hungry and wishes to eat. At this point, he reaches for the forks on his either side and once he’s got both of them, proceeds to eat. Once the eating is done, the philosopher then puts the forks down, so that they’re available for his neighbor.

Implementation:- We model each of our philosophers as classes that implement the Runnable interface so that we can run them as separate threads. Each Philosopher has access to two forks on his left and right sides:

1. Philosopher.java

/**

* @author gmalaker

* We model each of our philosophers as classes that implement the Runnable interface so that we can run them as separate threads.

* Each Philosopher has access to two forks on his left and right sides:-

* We also have a method that instructs a Philosopher to perform an action – eat, think, or acquire forks in preparation for eating:

*

*/

public class Philosopher implements Runnable {

// The forks on either side of this Philosopher

private Object leftFork;

private Object rightFork;

public Philosopher(Object leftFork, Object rightFork) {

this.leftFork = leftFork;

this.rightFork = rightFork;

}

private void doAction(String action) throws InterruptedException {

System.out.println(Thread.currentThread().getName() + " " + action);

Thread.sleep(((int) (Math.random() * 100)));

}

@Override

public void run() {

try {

while (true) {

// thinking

doAction(System.nanoTime() + ": Thinking");

synchronized (leftFork) {

doAction(System.nanoTime() + ": Picked up left fork");

synchronized (rightFork) {

// eating

doAction(System.nanoTime() + ": Picked up right fork - eating");

doAction(System.nanoTime() + ": Put down right fork");

}

// Back to thinking

doAction(System.nanoTime() + ": Put down left fork. Back to thinking");

}

}

} catch (InterruptedException e) {

Thread.currentThread().interrupt();

return;

}

}

}

2. DiningPhilosophers.java

public class DiningPhilosophers {

public static void main(String[] args) throws Exception {

Philosopher[] philosophers = new Philosopher[5];

Object[] forks = new Object[philosophers.length];

for (int i = 0; i < forks.length; i++) {

forks[i] = new Object();

}

for (int i = 0; i < philosophers.length; i++) {

Object leftFork = forks[i];

Object rightFork = forks[(i + 1) % forks.length];

philosophers[i] = new Philosopher(leftFork, rightFork);

Thread t = new Thread(philosophers[i], "Philosopher " + (i + 1));

t.start();

}

}

}

NOTE:- Run the above programfor 3 or more times the deadlock will appear

This is a sample output for a deadlock that appeared:-

Philosopher 1 8487540546530: Thinking

Philosopher 2 8487542012975: Thinking

Philosopher 3 8487543057508: Thinking

Philosopher 4 8487543318428: Thinking

Philosopher 5 8487544590144: Thinking

Philosopher 3 8487589069046: Picked up left fork

Philosopher 1 8487596641267: Picked up left fork

Philosopher 5 8487597646086: Picked up left fork

Philosopher 4 8487617680958: Picked up left fork

Philosopher 2 8487631148853: Picked up left fork

3. DiningPhilosophers_WithoutDeadlock.java

/**

* @author gmalaker

* Resolving the Deadlock:- the primary reason for a deadlock is the circular wait condition where each process waits upon a

* resource that’s being held by some other process. Hence, to avoid a deadlock situation we need to make sure that the circular

* wait condition is broken. There are several ways to achieve this, the simplest one being the follows:

* All Philosophers reach for their left fork first, except one who first reaches for his right fork.

* The change comes in lines 25-29 of the above code, where we introduce the condition that makes the last philosopher

* reach for his right fork first, instead of the left. This breaks the circular wait condition and we can avert the deadlock

*/

public class DiningPhilosophers_WithoutDeadlock {

public static void main(String[] args) throws Exception {

final Philosopher[] philosophers = new Philosopher[5];

Object[] forks = new Object[philosophers.length];

for (int i = 0; i < forks.length; i++) {

forks[i] = new Object();

}

for (int i = 0; i < philosophers.length; i++) {

Object leftFork = forks[i];

Object rightFork = forks[(i + 1) % forks.length];

if (i == philosophers.length - 1) {

// The last philosopher picks up the right fork first

philosophers[i] = new Philosopher(rightFork, leftFork);

} else {

philosophers[i] = new Philosopher(leftFork, rightFork);

}

Thread t = new Thread(philosophers[i], "Philosopher " + (i + 1));

t.start();

}

}

}

Run this program for 3 or more times, and deadlock will never appear in this case.

Please let me know in case of any clarifications required. Thanks!

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