GATE / CS ARCHIVE

GATE CSE 2014 SET 2 / QUESTION 31

GATE CSE 2014 Set 2 Q31

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2014 Set 2 paper

GATE 2014 Set 2, Q31 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 31, GATE 2014: Producer-consumer pseudocode starts with semaphores n=0, s=1. Producer repeats produce(); wait(s); addToBuffer(); signal(s); signal(n). Consumer repeats wait(s); wait(n); removeFromBuffer(); signal(s); consume(). Which is true? A Producer can add but consumer can never consume. B Consumer removes at most one item. C Deadlock occurs if consumer acquires s when the buffer is empty. D n must start at 1 rather than 0 for deadlock-free operation.
Tap to enlarge
Your answer
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.

Original paper, page 37 · Answer key