The idea is that if the direction is east, and a baboon wants to travel west, the block_east interrupt can be set to block any additional baboons on the east side. east = semaphore(1); // east side control west = semaphore(1); // west side control mutex = semaphore(1); // provide mutual exclusion for four variables rope = semaphore(5); // ensure only 5 baboons are on the rope at a time count = 0; // number of baboons granted access to the rope block_east = 0 // interrupt for east side to block itself block_west = 0 // interrupt for west side to block itself wait_west = 0 // queue count for west side wait_east = 0 // queue count for east side n = 10 // require 10 baboons to accumulate on the west side before interrupting ------------------------------------------------------------------------------- mutex.signal() wait_east++; // increment queue count mutex.signal() east.wait() mutex.wait() // check variables to see if baboon can get on rope if(count == 0 && block_west == 0) west.wait() // block the west if not already blocked if(count > 0 && wait_west > n) block_east = 1; // wait_west interrupt is set, block east side // count must be greater than zero to ensure at least one baboon // to cross and avoid possible deadlock block_west = 0; // clear interrupt count++; // increment count on rope / waiting to get on rope if(block_east == 0) east.signal() // allow east to continue, unless interrupt set mutex.signal() // end check rope.wait() // use semaphore to allow more than 5 baboons to line up, // only let 5 at a time through ## CS - baboon is crossing rope mutex.wait() //once baboon is across rope, update variables count--; //decrement count of baboons on rope / waiting to get on rope wait_east--; //decrement queue count if(count == 0) // if the rope is empty west.signal() rope.signal() // signal completion mutex.signal()