in this post, we are going to see about Dining Philosophers Problem in os which is another classical problem of process synchronization.In computer science, the dining philosophers problem is an example problem used in concurrent algorithm design to illustrate synchronization issues and techniques for resolving them.
Dining Philosophers Problem says suppose there is five Philosopher sitting around a circular table and center of the table we have rice bowl and there are five chopsticks each placed between the philosophers.At a given point in time, philosophers can either eat or think, philosophers can’t do both the thing together.
when a philosopher wants to eat he has two chopsticks. at the time p1 is eating p2 can’t eat because one of the chopsticks has to be taken by philosopher p1.when a philosopher wants to think he keeps both chopsticks.
each philosopher can behave as follows
- think until the left fork is available; when it is, pick it up;
- think until the right fork is available; when it is, pick it up;
- when both forks are held, eat for a fixed amount of time;
- then, put the right fork down;
- then, put the left fork down;
- repeat from the beginning.
solution of Dining Philosophers Problem in os
- philosophers can pick up chopsticks when both left and right are available.
- allow only 4 philosophers to sit.That way, if all the four philosophers pick up four chopsticks, there will be one chopstick left on the table. So, one philosopher can start eating and eventually, two chopsticks will be available. In this way, deadlocks can be avoided.
wait(stick[(i+1) % 5]); // Eat
signal(stick[(i+1) % 5]); //think
An array of five semaphores stick for each of the five chopsticks.
you may also like this…..