ThreadMentor: Visualizing Roller Coaster

Suppose we run this program with five passengers and car capacity 3. Initially, the History Graph window is similar to the following one. It shows that all five passenger threads are blocked on the WaitingQueue semaphore (i.e., joining the waiting line). On the other hand, the car thread is running and has signaled once, allowing one passenger to check in.

This is shown in the main window. We see that semaphore WaitingQueue has all passenger threads waiting, while the other semaphores have none.

The following is the detail of each semaphore.

WaitingQueue CheckIn Boarding
Riding Unloading

Run the program a little longer, we see that the car thread signals three times as indicated by the first three SS tags. Each of these three signals releases one passenger threads from the semaphore WaitingQueue. Then, the car thread waits (i.e., the SW tag) for boarding.

The released passenger threads are Passenger1, Passenger2 and Passenger3, each of which goes to the CheckIn semaphore. Note that each of these three passenger threads has a SW and a SE at the very beginning, because they have waited and released. Now look at passenger thread Passenger1 carefully. It has a second pair of SW and SE tag, which means this passenger passed the CheckIn semaphore. When this passenger thread releases the CheckIn semaphore by executing a signal (i.e., tag SS), it causes passenger thread Passenger2 to continue. Thread Passenger2 follows the same step that checks itself in, releases semaphore CheckIn, and waits in the car (i.e., the last SW tag). Return to passenger thread Passenger1. After releases the CheckIn semaphore, it waits on semaphore Riding (i.e., on the car), and this is the meaning of the last SW tag of passenger thread Passenger1. Hence, at the right end of the bars, we have two threads in the car: Passenger1 and Passenger2, one thread checking in (i.e., Passenger3), and two threads in the waiting queue: Passenger4 and Passenger5. The car thread is waiting on semaphore Boarding. This fact is recorded by the five semaphores below:

WaitingQueue CheckIn Boarding
Riding Unloading

Continue with the boarding process. The previous History Graph shows that threads Passenger1 and Passenger2 are on board. In the following History Graph window, we see clearly that thread Passenger1 signals thread Passenger2, and thread Passenger2 signals thread Passenger3. This brings two passengers on board the car. Since thread Passenger3 is the third passenger thread on board the car, it is a full count and thread Passenger3 signals the car thread, indicating all passengers are on board. All on board passenger threads executed a wait on semaphore Riding. The last pair of tags SE and SS indicates the car is moving.

Once the car thread reaches its last SS tag, it is the end of this ride and the beginning of the unloading process. The following History Graph depicts this process. The car thread signals a passenger, in this case Passenger1, indicating the end of a ride, and waits until the signaled passenger gets off the car. When the signaled passenger gets off the car, it signals the car thread so that the car can process the next passenger. The History Graph below indicates that threads Passenger1, Passenger2 and Passenger3 get off the car, in this order.

Continue the execution of this program for a while, and we will have a History Graph similar to the following one. It shows the next check in and boarding sections. More precisely, after completes the previous unloading process, the car thread signals semaphore WaitingQueue to release three threads for them to proceed to check in. The released threads are Passenger4, Passenger5 and Passenger1. These threads must all go through the CheckIn semaphore. The first released thread by the car, thread Passenger4, is allowed to exit semaphore CheckIn because of the last signal to semaphore CheckIn made by passenger thread Passenger3 when it was the last one to pass the check in point and increased the counter by one. Therefore, you see a long line segment connecting a SS on thread Passenger3's history bar to a SE tag on thread Passenger4's history bar. Other than this strange line segment, the checking in and boarding sections are similar to the previous one.