读书人

哪位高手能帮忙解一下Baboon crossing

发布时间: 2012-04-08 14:38:30 作者: rapoo

谁能帮忙解一下Baboon crossing problem
A student majoring in anthropology and minoring in computer science has embarked on a research project to see if African baboons can be taught about deadlocks. He locates a deep canyon and fastens a rope across it, so the baboons can cross hand-over-hand. Several baboons can cross at the same time, provided that they are all going in the same direction. If eastward moving and westward moving baboons ever get onto the rope at the same time, a deadlock will result (the baboons will get stuck in the middle) because it is impossible for one baboon to climb over another one while suspended over the canyon. If a baboon wants to cross the canyon, it must check to see that no other baboon is currently crossing in the opposite direction. Write a program using semaphores that avoids deadlock. Do not worry about a series of eastward moving baboons holding up the westward moving baboons indefinitely.

一个经典IPC问题,baboon有五种状态,east ledge, east rope,middle rope, west rope, west ledge,要避免deadlock,最后得到的结果类似这样的print out
Ape #1 on East Ledge.
Ape #2 on West Ledge.
Ape #3 on East Ledge.
Ape #1 moves West. Ape #1 on East Rope.
Ape #1 moves West. Ape #1 on Middle Rope.
Ape #3 moves West. Ape #3 on East Rope.
Ape #1 moves West. Ape #1 on West Rope.
Ape #1 moves West. Ape #1 on West Ledge.
Ape #3 moves West. Ape #3 on Middle Rope.
Ape #3 moves West. Ape #3 on West Rope.
Ape #3 moves West. Ape #3 on West Ledge.
Ape #2 moves East. Ape #2 on West Rope.
Ape #2 moves East. Ape #2 on Middle Rope.
Ape #3 moves East. Ape #3 on West Rope.
...

让几个baboon不断过50次.... 我写了几次,总是有deadlock.....

[解决办法]
完全没看懂

读书人网 >C++

热点推荐