"The Little Book of Semaphores" (2nd Ed. by Allen B. Downey, section 4.5.1) as well as Wiki (link) mentioned that a trivial solution (as shown below) to the 'Cigarette smokers problem' will cause a deadlock. Somehow I couldn't wrap my head around it. Pls help me undersatnd how deadlock can occur for the below solution. P.S. We assumed agent code cannot be modified and We're free to use semaphores and other variables as needed
Code sample (from the book):
Agent A Agent B Agent C
1 agentSem.wait() agentSem.wait() agentSem.wait()
2 tobacco.signal() paper.signal() tobacco.signal()
3 paper.signal() match.signal() match.signal()
Smoker I Smoker II Smoker III
1 tobacco.wait() paper.wait() tobacco.wait()
2 paper.wait() match.wait() match.wait()
3 agentSem.signal() agentSem.signal() agentSem.signal()
Assume semaphores 'tobacco', 'paper', and 'match' are initialized with
zero, and 'agentSem' is initialized with one.