Anonymous user menu

What if the order of the first two wait instructions in the consumer process were reversed in bounded buffer solution

In the bounded buffer solution shown in Figures  5.10 if the order of the first two wait instructions in the consumer process were reversed. In other words, the consumer process would begin:

-----------------------------------------------------------

Consumer

do {

wait(full);

wait(mutex);

//consume item

//update “Out”

signal(mutex);

signal(empty);

} while (true);

-----------------------------------------------------------------------------

do { 
wait ( mutex );
wait ( full );…

Would the solution still be correct? Explain why or why not.

1Comment
Arul @innovwelt
6 May 2016 01:40 am

"wait/signal (mutex)" has been introduced, only to make sure that a single process accesses the Queue.

The solution would be wrong (leads to deadlock), as per the below scenario.

  • initially, let the Queue be empty i.e, full = 0, empty = BUFFER SIZE.
  • Now, the consumer has to wait and producer has to produce.
  • according to the definition of the locks 'full' (=0), 'empty' (=BUFFER SIZE)
    1. wait(full) will not the let the consumer to pass through
    2. wait(empty) will let the producer to pass through and add the item to Queue
  • when we change the order in Consumer process, "assume" the consumer will acquire the 'mutex' straight away. Now consumer has the call 'wait(full)' which will stop the consumer process.
  • In producer side, Since the 'mutex' is already locked by consumer, the producer cannot add any item to Queue. hence, a deadlock.

producer-consumer-deadlock