Monday, March 02, 2009

Monkeys: pt 5

What do Monkeys Eat??

First off, interesting fact. Monkeys are generally herbivores or omnivores. Interestingly, there is a particular type of primate known as the Tarsier that is actually a carnivore. It is also incredibly creepy looking. (see creepy looking thing just below) The Tarsier lives in the islands of Southeast Asia, although fossil records show that it once may have roamed most continents. To be more clear, are mostly "insectivorous," which of course means they eat insects, but also partake of a tasty small bird, snake, lizard or bat, as the fancy hits them.

Most monkey herbivores are in the region of Asia. It's difficult being a herbivore. With more intestines and stomach acid than other monkeys, including "little compartments, called saccules, and in each one there is bacteria and different kinds of enzymes to break down the plant matter," there is quite a bit of work in being a monkey that eats plants.

On the other hand, most monkey omnivores are in the Americas. Some are even aficionados of shellfish. They can be rather aggressive in their tactics, indeed "several species of monkeys, and chimpanzees, but not the other apes, have been known to attack and eat other monkeys. Baboons, the most adept hunters on the ground, often eat meat and sometimes manage to kill small antelope." Yes, they eat each other as well as small antelope.

Monkeys do also eat bananas. Here is a wonderfully bizarre article about why monkeys were originally associated with bananas. As with many things, it is the fault of white people (which may of course be true. I certainly don't know). This particular argument is that the Americans took over the Philippines, they ate bananas there, and so to mock them, cartoonists showed monkeys eating bananas, i.e., equating the locals with bananas.

While it is certainly true that cartoonists were big on racial stereotypes during that time period, it seems remarkable to me that I've never heard this before (especially since I had almost an entire class in middle school on racial stereotypes in cartoons - it was supposed to be American history).

Far more bizarre, the monkey/banana association has led to a classic programming/logic problem known as the "Monkey and banana problem." (here's a breakdown for those who care, taken from here: "The Monkeys and Bananas Problem
Consider the following synchronization problem. A boat delivers bananas to a desert island which is inhabited only by monkeys. The boat delivers one load of bananas at a time, but the load has a variable number of bananas in it; when it arrives, it calls the deliver(int nbananas) function, where nbananas specifies how many are delivered (at least one is always delivered). Bananas are placed in a crate on the beach. When a monkey is hungry, it goes to the crate. If there are enough bananas in the crate, it takes what it needs; otherwise, it joins a queue of monkeys waiting for the boat. The monkeys are greedy and are not always satisfied with just one banana. A monkey wanting to get nbananas bananas calls monkey(int nbananas). Calling monkey should cause it to wait in the queue until it gets nbananas bananas, then leave. A monkey will to stay at the head of the queue until it has all the bananas it wants, rather than, for instance, grabbing 3 out of 4 bananas and then going to the back of the queue to wait for its final banana.
Devise a solution for the monkeys-and-bananas problem which follows the rules given above, and incorporates
correct concurrency control using only semaphores.
We can use a semaphore as a counter to count the number of bananas. The head monkey needs to be treated
specially, since it has to stay at the head of the queue until it gets all the bananas it wants. Therefore we
make the head monkey wait on the bananas semaphore and create a separate semaphore for the rest of the
monkeys, which we call queue. The head monkey signals queue when it has all the bananas it wants, as
long as at least one monkey is waiting in the queue. Since we cannot determine the queue value ourselves,
we need a separate variable to count how many monkeys are waiting by the crate, and we protect this
variable with the semaphore mutex. Here's a solution:
sem bananas = 0; // could initialise to any value
sem queue = 0; // all monkeys except head monkey wait here
int monkeys = 0;
sem mutex; // to protect "monkeys"
// put "nbananas" bananas in the crate
void deliver(int nbananas) {
for (int i=0; isignal(bananas);
}
// monkey wants "nbananas" bananas
void monkey(int nbananas) {
wait (mutex);
monkeys++;
if (monkeys > 1) { // i.e. there was already a head monkey
signal(mutex);
wait(queue);
}
else
signal(mutex);
// now I am the head monkey
for (int i=0; iwait(bananas);
// wake up the next monkey
wait(mutex);
monkeys--;
if (monkeys > 0)
signal(queue);
signal(mutex);
}"


Yeah. I don't know why, but that doesn't seem terribly complicated to me, except for the obvious issue that each monkey can be a bottomless pit and I suppose to go anywhere, you would have to specify in the program that once a crate was empty, the head monkey had to leave or at least move to the back of the line. Sure, there's the uncertainty, but you always have that where free will is concerned. But, clearly, very clearly, I digress here.

Finally, I will share the story of a crazy Canadian fellow who decided to subsist on monkey food pellets. He lasted a week and it didn't go well. He did lose a few pounds, but evidently lost his mind in the process.