Question

Dining Philosophers Problem (JAVA) We have a table of N philosophers. As with the traditional problem,...

Dining Philosophers Problem (JAVA)

We have a table of N philosophers. As with the traditional problem, they are sitting around a circular table with a plate in front of them, a bowl of noodles in the center to share and a fork between each philosopher. However, the senior citizen philosophers, in addition to think-hungry-eat states, have two more states, sleep, and enlightened thinking. A philosopher has an k% chance of entering the enlightened state while thinking. If they become hungry while in the enlightened state, then they can pick up any available fork rather than being limited to the two
besides them. When done, they put the forks back where they got them. A philosopher has an s% chance of entering a sleep state while doing something else – they can be sleep-thinking, sleep-hungry, sleep-eating. Once in that state, they will not progress into the next state unless they are nudged by one of the philosophers next to them – they will stay sleeping until nudged. Enlightenment and sleeping are mutually exclusive states.
Deadlock management is a key feature of this problem. You can try to handle this through some kind of deadlock avoidance, or deadlock recovery. The possibility of sending interrupt signals, while outside of the framework of waiting and signaling is not precluded if you can figure out how to do it in the implementation language and synchronization tool.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class DiningPhilosophers {

    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();

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Dining Philosophers Problem (JAVA) We have a table of N philosophers. As with the traditional problem,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT