In 1965, Dijkstra posed and solved a synchronization problem he called the dining philosophers problem.

Since that time, everyone inventing yet another synchronization primitive has felt obligated to demonstrate how wonderful the new primitive is by showing how elegantly it solves the dining philosopher’s problem.

The problem can be stated quite simply as follows. 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 a philosopher consists of alternate periods of eating and thinking. (This is something of an abstraction, even for philosophers, but the other activities are irrelevant here.)

When a philosopher gets hungry, she tries to acquire her left and right forks, one at a time, in either order. If successful in acquiring two forks, she eats for a while, then puts down the forks, and continues to think.

#define N 5

void philosopher(int i)


     while (TRUE)




           take_fork((i+1) % N);



           put_fork((i+1) % N);



Solution Dining Philosophers Problem Using Semaphore

Typeid int semaphore;

semaphore fork [5] = {1};

int i;

void philosopher (int i)


         while (true)



                  wait (fork[i]);

                  wait (fork [(i+1) mod 5]);


                  signal(fork [(i+1) mod 5]);