Similar questions 1 more →
Synchronization & Concurrency

Answer & solution
Official answer: C
1. Set up the reasoning. Interpret s as the mutex and n as the number of available items. Trace the particular interleaving where the consumer runs first and locks the empty buffer.
2. Derive the result. If the consumer acquires s while n=0, it blocks on n while retaining s. The producer needs s before adding an item and signaling n, so neither can proceed. That is the deadlock in C. Correct ordering is to wait for n before acquiring the buffer mutex; inventing an initial item by setting n=1 is not a valid repair.
3. Check the interpretation. The consumer holds the very lock the producer requires to make n positive. The proper repair is to wait on n before locking s, not to pretend that an item exists by setting n=1.