每位哲学家都拿到自己的筷子并等待使用其邻座的筷子时,就会发生实际的死锁。
编号为 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 |