You are on page 1of 4

Dining Philosophers Problem

What's the dining philosopher's problem?

The dining philosophers problem describes a group of philosophers sitting at a table


doing one of two things - eating or thinking. While eating, they are not thinking, and
while thinking, they are not eating. The philosophers sit at a circular table each with
a bowl of spaghetti. A chopstick is placed in between each philosopher, thus each
philosopher has one chopstick to his or her left and one chopstick to his or her right.
As spaghetti is difficult to serve and eat with a single chopstick, it is assumed that a
philosopher must eat with two chopsticks. The philosopher can only use the
chopstick on his or her immediate left or right.

The philosophers never speak to each other, which creates a dangerous possibility
of deadlock. The deadlock could occur if every philosopher holds a left chopstick
and waits perpetually for a right chopstick (or vice versa).

Figure 1
the story of the dining philosophers as an exercise in concurrent programming in the
early 1970s.

this problem has attracted and challenged both theoreticians and programmers, and
a variety of different solutions have been developed, most of them using some kind
of synchronization mechanism (typically a semaphore) to control accesses to
chopsticks by hungry philosophers.

• Devise an algorithm that will allow the philosophers to eat :


1. No two philosophers can use the same fork at the same time ( mutual
exclusion).
2. No philosopher must starve to death ( avoid deadlock and starvation).

Figure 2
Solution 1: semaphore

Each philosopher picks up first the fork on the left and then the fork on the right.

After the philosopher is finished eating, the two forks are replaced on the table.

/ * program dining philosophers * /


Semaphore fork [5 ] = { 1 } ;
Void philosopher ( int i )
{
While ( true ) {
Think () ;
Wait (fork[ i ] ) ;
Wait (fork [ ( i +1 ) mod 5 ] ) ;
eat () ;
Signal ( fork [ ( i + 1 ) mod 5 ] ) ;
Signal (fork [ i ] ) ;
}
Void main ()
{
Parbegin (philosopher( 0 ) , philosopher (1 ) , philosopher ( 2 ) ,
philosopher ( 3 ) , philosopher ( 4 ) ;
}

Figure 3

This solution alas leads to deadlock !!

If all of the philosophers are hungry at the same time, they all sit down, they all pick
up the fork on their left, and they all reach out for the other fork, which is not there.

In this undignified position, all philosophers starve.


Solution 2: semaphore with no deadlocks

We could consider adding an attendant who only allows four philosophers at a time
into the dining room.

With at most four seated philosophers at least one philosopher will have access to
two forks.

this solution is free of deadlock and starvation.

//*program diningphilosophers * //

Semaphore fork [ 5 ] = { 1 } ;
Semaphore room = { 4 } ;
int i ;
Void philosopher ( int i )
{
While ( true ) {
Think () ;
Wait ( room );
Wait ( fork [ i ] ) ;
Wait ( fork [ ( i + 1 ) mod 5 ] ) ;
eat ( ) ;
Signal ( fork [ ( i + 1 ) mod 5 ] ) ;
Signal ( fork [ i ] ) ;
Signal (room) ;
}
}
Void main ()
{
Parbegin ( philosopher (0) , philosopher ( 1) , philosopher (2) ,
philosopher (3) , philosopher (4) ;

Figure 4

Name: Amany Alharbey

ID: 558

You might also like