GateAI
Concurrency and Synchronization

GATE CSE 2014 Set 2 · Q31

MCQ2 marksMedium

Time

Consider the procedure below for the *Producer-Consumer* problem which uses semaphores:semaphore n = 0; semaphore s = 1; void producer() { while(true) { produce(); semWait(s); addToBuffer(); semSignal(s); semSignal(n); } } void consumer() { while(true) { semWait(s); semWait(n); removeFromBuffer(); semSignal(s); consume(); } }Which one of the following is TRUE?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty.

Concurrency and Synchronization

Let us trace what happens when the consumer runs first while the buffer is empty:

  1. Initial values: s=1s = 1, n=0n = 0.

  2. The consumer executes semWait(s). Since s=1s = 1, ss becomes 00 and the consumer proceeds.

  3. The consumer then executes semWait(n). Since n=0n = 0, the consumer is blocked waiting for nn to become positive.

  4. Now, the producer tries to produce an item. It executes produce() and then semWait(s).

  5. Since s=0s = 0 (held by the blocked consumer), the producer is also blocked waiting for ss.

  6. Both processes are waiting on each other and neither can proceed: the consumer holds ss and waits for nn, while the producer holds nothing, waits for ss, and is the only entity that can signal nn.

Thus, a deadlock occurs if the consumer acquires semaphore s when the buffer is empty. Hence, option (C) is the correct statement.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Concurrency and Synchronization previous year question is Q31 of the GATE CSE 2014 Set 2 paper. Find more GATE CSE PYQs on Concurrency and Synchronization in Operating Systems, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Concurrency and Synchronization PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Concurrency and Synchronization in GATE CSE

Concurrency and Synchronization has 18 GATE CSE PYQs across 15 papers from 2012 to 2026. 9 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2014 Set 2.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2014 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2014 Set 2 · Q31 · MCQ

Consider the procedure below for the *Producer-Consumer* problem which uses semaphores: semaphore n = 0; semaphore s = 1; void producer() { while(true) { produce(); semWait(s); addToBuffer(); semSignal(s); semSignal(n); } } void consumer() { while(true) { semWait(s); semWait(n); removeFromBuffer(); semSignal(s); consume(); } } Which one of the following is TRUE?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.