This is an animation of the dining philosophers. Five philosophers, represented by circles, sit around a bowl of spaghetti. Each thinks for a while, gets hungry, and wants to eat. Thinking philosophers are outline black, hungry philosophers are solid green, and eating ones are solid blue. There are five forks alternating with the philosophers around the table. A philosopher must have sole possession of both its left and right forks in order to eat. The forks are shown as gray lines while a philosopher eats.
private void eat() { int napping = 1 + (int) random(napEat); xa.color("phil"+id, Color.blue); // animation xa.fill("phil"+id, xa.SOLID); // animation nap(napping); } public void run() { while (true) { think(); xa.color("phil"+id, Color.green); // animation xa.fill("phil"+id, xa.SOLID); // animation ds.takeForks(id); eat(); ds.putForks(id); xa.fill("phil"+id, xa.OUTLINE); // animation xa.color("phil"+id, Color.black); // animation } } public void takeForks(int i) { P(mutex); state[i] = HUNGRY; test(i); V(mutex); P(self[i]); xa.color("fork"+i, Color.gray); // animation xa.color("fork"+right(i), Color.gray); // animation } public void putForks(int i) { P(mutex); state[i] = THINKING; xa.color("fork"+i, Color.white); // animation xa.color("fork"+right(i), Color.white); // animation test(left(i)); test(right(i)); V(mutex); }At the bottom of the animation window is a button and text field showing the default values of some of the command line arguments. Alter these as needed and click the button.
-p: number of philosophers
-R: number of seconds to run
© 1998 Stephen J. Hartley