実デッドロックは、哲学者全員がそれぞれに自分の箸を手にしていて、隣の哲学者の箸が使い終わるのを待つときに発生します。
哲学者 0 は箸 0 を手にしていて、箸 1 を待つ |
哲学者 1 は箸 1 を手にしていて、箸 2 を待つ |
哲学者 2 は箸 2 を手にしていて、箸 3 を待つ |
哲学者 3 は箸 3 を手にしていて、箸 4 を待つ |
哲学者 4 は箸 4 を手にしていて、箸 0 を待つ |
この状況では誰も食事をすることができず、哲学者はデッドロック状態となります。プログラムを何回か実行します。実行が滞ることもあれば、最後まで実行されることもあることがわかります。
食事する哲学者プログラムを実行して、実行が完了するか、またはデッドロックになるかを確認します。 次の実行例に見られるように、実行は滞る可能性があります。
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) CTRL-C を押して実行を終了します