GATE / CS ARCHIVE

GATE CSE 2013 / QUESTION 39

Binary-semaphore producer-consumer rendezvous

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2013 paper

GATE 2013, Q39 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 39, GATE 2013: Producer X computes a[i]=f(i), then ExitX; consumer Y calls EntryY, then computes b[i]=g(a[i]), for i=0,…, n−1. Binary semaphores R, S initially 0. Which implementation synchronizes them? A Exit=P(R), V(S);Entry=P(S), V(R). B Exit=V(R), V(S);Entry=P(R), P(S). C Exit=P(S), V(R);Entry=V(S), P(R). D Exit=V(R), P(S);Entry=V(S), P(R).
Tap to enlarge
Your answer
Answer & solution

Official answer: C

In C, the consumer first signals S to indicate readiness, then waits on R; the producer waits on S before signaling R after its item has been computed. The consumer reads the item only after that R handshake. Since the next S is not issued until the preceding consumer iteration finishes, signals cannot outrun the corresponding item and collapse in a binary semaphore. A deadlocks immediately because both sides initially wait on zero semaphores. B lacks a readiness handshake, so multiple signals can collapse. In D, the producer can signal R for the next item before the consumer consumes the preceding R, again collapsing signals. Thus C provides the required synchronization.

A binary semaphore stores only whether a signal is pending, not an unbounded count. The consumer’s V(S) in C grants permission for one producer notification, then its P(R) waits for the corresponding completed a[i]. The producer cannot notify the next item without another S grant, which the consumer issues only in its next iteration. This prevents two notifications collapsing into one. In D, after the first readiness signal the producer can release R twice before the consumer takes R even once, losing a notification and potentially blocking a later consumer iteration.

Original paper, page 9 · Answer key