Oracle® Solaris Studio 12.4: Thread Analyzer User's Guide

Exit Print View

Updated: December 2014
 
 

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 0 is holding chopstick 0, but is waiting for chopstick 1

  • Philosopher 1 is holding chopstick 1, but is waiting for chopstick 2

  • Philosopher 2 is holding chopstick 2, but is waiting for chopstick 3

  • Philosopher 3 is holding chopstick 3, but is waiting for chopstick 4

  • Philosopher 4 is holding chopstick 4, but is waiting for chopstick 0

In this situation, nobody can eat and the philosophers are in a deadlock. Run the program a number of times. You will see that the program might hang sometimes, and run to completion at other times. The program might hang as shown in the following sample run:

prompt% cc din_philo.c
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