In Little Book of Semaphores:
4.1.6 Finite buffer producer-consumer solution
Finite buffer consumer solution
1 items.wait () 2 mutex.wait () 3 event = buffer .get () 4 mutex. signal () 5 spaces . signal () 6 7 event. process ()
Finite buffer producer solution
1 event = waitForEvent () 2 3 spaces .wait () 4 mutex.wait () 5 buffer .add(event) 6 mutex. signal () 7 items. signal ()
In order to avoid deadlock, producers and consumers check availability before getting the mutex.
Why does "producers and consumers check availability before getting the mutex" avoid deadlock?
I have learned some methods preventing deadlock by making one of the four necessary conditions of deadlock not hold (mutual exclusion, hold and wait, nonpreemtible, circular waiting. c.f. Section 7.3 in Operating Systems Concepts), and some methods avoiding deadlock (c.f. Section 7.4). I was wondering what condition the solution in the quote makes not hold, or what deadlock avoidance method it uses?
Thanks.