Oracle® Solaris Studio 12.4: スレッドアナライザユーザーズガイド

印刷ビューの終了

更新: 2014 年 12 月
 
 

哲学者がデッドロックに陥るしくみ

    デッドロックが実際に起こるのは、哲学者全員が自身の箸を手に持ち、隣の哲学者の箸が使用できるようになるのを待機している、次のような状況です。

  • 哲学者 0 は箸 0 を手に持って箸 1 を待機している

  • 哲学者 1 は箸 1 を手に持って箸 2 を待機している

  • 哲学者 2 は箸 2 を手に持って箸 3 を待機している

  • 哲学者 3 は箸 3 を手に持って箸 4 を待機している

  • 哲学者 4 は箸 4 を手に持って箸 0 を待機している

この状況では誰も食事できず、哲学者たちはデッドロック状態に陥ります。プログラムを何度も実行するとわかります。つまりこのプログラムは、ハングアップすることもあれば、最後まで実行できることもあるのです。次の実行例は、このプログラムがハングアップする様子を示しています。

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