Sun Studio 12: Thread Analyzer User's Guide

3.2.1 How the Philosophers Can Deadlock

An actual deadlock occurs when every philosopher is holding his own chopstick and waiting for the one from his neighbor to become available:

Philosopher zero is holding chopstick zero, but is waiting for chopstick one 

Philosopher one is holding chopstick one, but is waiting for chopstick two 

Philosopher two is holding chopstick two, but is waiting for chopstick three 

Philosopher three is holding chopstick three, but is waiting for chopstick four 

Philosopher four is holding chopstick four, but is waiting for chopstick zero 

In this situation, nobody can eat and the philosophers are in a deadlock. Rerun the program a number of times and you will see that the program may sometimes hang, or run to completion at other times.

Run the dining philosophers program and see whether it completes or deadlocks. It may hang as shown in the following sample run:

prompt% cc din_phil.c -mt
prompt% a.out
Philosopher 0 is done thinking and now ready to eat.
Philosopher 2 is done thinking and now ready to eat.
Philosopher 2: got right  chopstick 2
Philosopher 2: got left chopstick 3
Philosopher 0: got right  chopstick 0
Philosopher 0: got left chopstick 1
Philosopher 0: eating.
Philosopher 4 is done thinking and now ready to eat.
Philosopher 4: got right  chopstick 4
Philosopher 2: eating.
Philosopher 3 is done thinking and now ready to eat.
Philosopher 1 is done thinking and now ready to eat.
Philosopher 0: got right  chopstick 0
Philosopher 3: got right  chopstick 3
Philosopher 2: got right  chopstick 2
Philosopher 1: got right  chopstick 1
(hang)

Execution terminated by pressing CTRL-C