Karel's World of Dining Philosophers

Don't add any walls or beepers to this world. It will be taken care of internally.

The story is as follows. Four philosopher robots meet to discuss philosophy and have lunch. They alternately eat and think. To eat they must pick up two forks (beepers) one from each side and then they move to the pile of spagetti in the middle to eat. To think they must put down the two forks and then move away from the table to think.

The problem is that there are only four forks so that at most two philosophers can eat at the same time.

However, we can reach a situation (rare) in which each philosopher holds one fork and seeks another, but it is held by another philosopher who will not release it. This is called "deadlock".

Note that each philosopher runs simultaneously with the others in a separate "thread of control." Since this is multi-threaded, after you click "Perform Task" you will need to also click "Resume" in the control dialog. You will probably want to speed up the execution also with the speed slider.

Here is the task code:

public static void main(String [] args)
{	World.placeBeepers(2, 2, 1);
	World.placeBeepers(2, 4, 1);
	World.placeBeepers(4, 2, 1);
	World.placeBeepers(4, 4, 1);
	Philosopher p1 = new Philosopher(2, 3, North);
	Philosopher p2 = new Philosopher(3, 2, East);
	Philosopher p3 = new Philosopher(4, 3, South);
	Philosopher p4 = new Philosopher(3, 4, West);
}

 

Back to the Experimental Page.

 

Last Updated: April 30, 2002