100% found this document useful (1 vote)
369 views2 pages

Dining Philosophers Problem

The dining philosophers problem involves five philosophers sitting around a table sharing five chopsticks. Each philosopher needs two chopsticks to eat, but if all grab one chopstick simultaneously a deadlock occurs as they wait forever for the other. Solutions include having philosophers wait to grab both chopsticks simultaneously or allowing only four philosophers to sit at the table.

Uploaded by

Kripesh Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
369 views2 pages

Dining Philosophers Problem

The dining philosophers problem involves five philosophers sitting around a table sharing five chopsticks. Each philosopher needs two chopsticks to eat, but if all grab one chopstick simultaneously a deadlock occurs as they wait forever for the other. Solutions include having philosophers wait to grab both chopsticks simultaneously or allowing only four philosophers to sit at the table.

Uploaded by

Kripesh Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Dining Philosophers Problem

The dining philosopher’s problem is another classic synchronization problem that is used to evaluate situations where
there is a need of allocating multiple resources to multiple processes.

What is the Problem Statement?


Consider five philosophers are sitting around a circular dining table. The dining table has five chopsticks and a bowl
of rice in the middle as shown in the below figure.

Dining Philosophers Problem


At any instant, a philosopher is either eating or
thinking. When a philosopher wants to eat, he uses
two chopsticks - one from their left and one from
their right.

When a philosopher wants to think, he keeps down


both chopsticks in their original place.

Here's the Solution


From the problem statement, it is clear that a
philosopher can think for an indefinite amount of
time. But when a philosopher starts eating, he has to
stop at some point in time.

The philosopher is in an endless cycle of thinking and


eating. An array of five semaphores, stick [5], for
each of the five chopsticks.
The code for each philosopher looks like:
While (TRUE){ signal(stick[i]);
wait(stick[i]); /* mod is used because if i=5, next
chopstick is 1 (dining table is circular) */ signal(stick[(i+1) % 5]);
wait(stick[(i+1) % 5]); /* think */
/* eat */ }

When a philosopher wants to eat the rice, he will wait for the chopstick at his left and picks up that chopstick. Then
he waits for the right chopstick to be available, and then picks it too. After eating, he puts both the chopsticks down.

But if all five philosophers are hungry simultaneously, and each of them picks up one chopstick, then a deadlock
situation occurs because they will be waiting for another chopstick forever.

The possible solutions for this are:


• A philosopher must be allowed to pick up the chopsticks only if both the left and right chopsticks are available.

• Allow only four philosophers to sit at the table. 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.

You might also like