Dining Philosophers Problem in os

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.


problem statement


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.

Dining Philosophers Problem in os
Dining Philosophers Problem in os

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


  1. philosophers can pick up chopsticks when both left and right are available.
  2. 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.

while(TRUE)

{

wait(stick[i]);

wait(stick[(i+1) % 5]); // Eat

signal(stick[i]);

signal(stick[(i+1) % 5]); //think

}

An array of five semaphores stick[5] for each of the five chopsticks.

 

you may also like this…..

what is process synchronization in os | critical section problem | semaphore | cooperating process

2 Replies to “Dining Philosophers Problem in os”

Leave a Reply

Your email address will not be published. Required fields are marked *