Explain about the Dining Philosopher Problem.

THE DINING PHILOSOPHER PROBLEMS
Problem:
Five philosophers are seated around a circular table. Each philosopher has a plate of spaghetti. The spaghetti is so slippery that a philosopher needs two forks to eat it. Between each pair of plates is one fork. The life of philosopher consists of alternative period of eating and thinking. when philosopher gets hungry, she tries to acquire her left and right forks, she eats for a while, then puts down the forks and continue to think.
The problem is that can we write a program for each philosopher that does what is supposed to do and never gets stuck?
Solution:
When the philosopher is hungry, it picks up a fork and wait for another fork, when get it eats for a while and put both forks back to the table.
#define N 5        //number of philosophers
void philosopher (int i) //i=0 to 4
{
while(TRUE)
{
think();                 //philosopher is thinking
take_fork(i);      //take left fork
take_fork((i+1)%N);       //take right fork
eat();     //eat spaghetti
put_fork(i);        //put left fork back on table
put_fork((i+1)%N);         //put right fork back on table
}
}

0 comments:

Feel free to contact the admin for any suggestions and help.