Run this program for a while and bring up the Thread Status window and History Graph. We learn that only thread Smoker(Tobacco) is running, and all other threads are blocked.
Let us examine thread Agent closely, because it is the key. The first SS tag of thread Agent causes thread Smoker(Match) to be released from semaphore Table (see its SE tag and the source window). Then, thread Smoker(Match) takes the ingredients (tobacco and paper), and releases the table which causes thread Agent to continue. While thread Smoker(Match) is smoking, thread Agent is preparing the next ingredients. From its history bar, thread Agent makes tobacco and match available and notifies thread Smoker(Paper). On the history bar of each smoker, we see three repeating tags in this order, SW (waiting for table), SE (ingredients being available) and SS (releasing the table). The agent also has three repeating tags in this order, SS (making ingredients available), SW (waiting for table) and SE (table being available). These patterns repeat until the execution completes.
The main window shows that there are four semaphores in the system: PaperMatch is 0 and has no waiting thread, MatchTobacco is 0 and has 1 waiting thread, TobaccoPaper is 0 and has 1 waiting thread, and Table is 0 and has 1 waiting thread.
Click on all four semaphores to bring up all semaphore windows. Let us look at who is waiting on which semaphore. First, thread Smoker(Match) has match and needs paper and tobacco. Because this thread is still waiting, it is no surprise that we can find thread Smoker(Match) on the waiting list of semaphore TobaccoPaper. Thread Smoker(Paper) is also blocked. Since it needs tobacco and match, it is on the waiting list of semaphore MatchTobacco. Finally, thread Agent is blocked and is on the waiting list of semaphore Table. This provides us with a reasonably clear picture that the agent just made paper and match available, which were picked up by thread Smoker(Tobacco). Then, thread Agent goes back to prepare for the next round, and waits for the table, which is likely still in the hand of thread Smoker(Tobacco) because he is in the process of taking ingredients and should release the table shortly.
|Semaphore PaperMatch||Semaphore TobaccoPaper|
|Semaphore MatchTobacco||Semaphore Table|