ThreadMentor: Visualizing Dining Philosophers: with Four Chairs

Run the program for a while and you will see the following shown in the History Graph window.

This shows that threads Philosopher0, Philosopher1, Philosopher2 and Philosopher3 have sit down and are trying to pick up chopsticks. Since the right-most philosopher (i.e., Philosopher3) has no right neighbor, his right chopstick is free and can be used. Therefore, Philosopher3 is eating. Philosopher4 is blocked because he could not get a chair!

Click on the Semaphore button in the upper-right corner of the main window to display all semaphores in the system. We have only one: FourChairs (see left figure below). Then, click on the line that contains FourChairs to bring up its semaphore window as shown in the right figure below. This shows that semaphore FourChairs has a counter value 0 and one waiting thread Philosopher4 (see the red traffic light icons). This justifies our observation.

Let us run this program for a little longer. The following is a screen-shots of the History Graph window. From the left edge of the History Graph window, we learn that Philosopher3 finished eating and released the chair, which caused Philosopher4 to take a seat. This relation is shown with a SS tag on the history bar of Philosopher3, a SE tag on the history bar of Philosopher4, and a link between them. Then, Philosopher3 came back and waited for a chair, and got it because Philosopher2 finished eating. After this, Philosopher3 picked up his left chopstick and is about to pick up his right one. Meanwhile, Philosopher0 is about to release his right chopstick (why?), Philosopher1 is waiting for a chair (the SW tag), Philosopher2 just got his chair due to Philosopher1, Philosopher3 is about to pick his right chopstick as mentioned earlier, and Philosopher4 is waiting for his right chopstick.

To see if Philosopher1 is waiting for a chair, brings up the semaphore window. See below. We learn that semaphore FourChairs has a zero counter and Philosopher1 is waiting. Thus, our reading of the History Graph window is correct. Note that semaphore FourChairs has no more than one waiting threads because it allows four philosophers to pass through and there are only five philosophers!