HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Why deadlock in cigarette smokers problem

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
problemwhydeadlocksmokerscigarette

Problem

"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.

Solution

Agent A consumes agentSem, and produces tobacco and paper.

That might make Smoker I smoke but he is late: Smoker II already took the paper and Smoker III took tobacco.

Now, all the smokers are stuck, and the agents as well.

Context

StackExchange Computer Science Q#67013, answer score: 4

Revisions (0)

No revisions yet.