Concurrent Programming - Course Work 3
A student studying anthropology and computer science has
embarked on a research project to see if baboons can be taught about deadlock.
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 baboons starting from
the South (Southern baboons) and baboons starting from the north (Northern
baboons) ever get onto the rope at the same time the baboons will get stuck
in the middle.
Below is the outline of the monitor class Rope,
used to synchronise access to the rope bridge.
class Rope { public synchronized void Sarrive() {…} // called by Southern baboons arriving at the South end of the rope
public synchronized void Sdepart() {…} // called by Southern baboons leaving the North end of the rope
public synchronized void Narrive () {…} // called by Northern baboons arriving at the North end of the rope
public synchronized void Ndepart() {…} // called by Northern baboons leaving the South end of the rope
}