GateAI
Concurrency and Synchronization

GATE CSE 2020 · Q34

MCQ2 marksMedium

Time

Each of a set of nn processes executes the following code using two semaphores a and b initialized to 1 and 0, respectively. Assume that count is a shared variable initialized to 0 and not used in CODE SECTION P.CODE SECTION P wait(a); count=count+1; if (count==n) signal(b); signal(a); wait(b); signal(b); CODE SECTION QWhat does the code achieve?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: A) It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.

Concurrency and Synchronization

This synchronization pattern implements a barrier (rendezvous):

  1. a is a mutex semaphore initialized to 11, protecting the critical section where count is incremented.

  2. When the first n−1n-1 processes finish CODE SECTION P:

    • Each enters the mutex, increments count, checks count == n (which evaluates to false), releases mutex a, and then blocks on wait(b) since semaphore b is initialized to 00.

  3. When the nn-th (last) process finishes CODE SECTION P:

    • It increments count to nn, evaluates count == n as true, and executes signal(b).

    • It then executes signal(a) and reaches wait(b). It consumes the signal on b and immediately performs signal(b).

  4. The signal(b) executed after wait(b) initiates a chain reaction (turnstile mechanism) that unblocks the next waiting process, which in turn unblocks the next, until all nn processes proceed to CODE SECTION Q.

Thus, no process can execute CODE SECTION Q until all nn processes have completed CODE SECTION P. Therefore, option (A) is correct.

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 Q34 of the GATE CSE 2020 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 2020.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2020

Share this solution

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

GateAI solution cardGATE CSE 2020 · Q34 · MCQ

Each of a set of n processes executes the following code using two semaphores a and b initialized to 1 and 0, respectively. Assume that count is a shared variable initialized to 0 and not used in CODE SECTION P. CODE SECTION P wait(a); count=count+1; if (count==n) signal(b); signal(a); wait(b); signal(b); CODE SECTION Q What does the code achieve?

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.