GateAI
Concurrency and Synchronization

GATE CSE 2012 · Q32

MCQ2 marksMedium

Time

Fetch_And_Add(X, i) is an atomic Read-Modify-Write instruction that reads the value of memory location X, increments it by the value i, and returns the old value of X. It is used in the pseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variable initialized to 0. The value of 0 corresponds to lock being available, while any non-zero value corresponds to the lock being not available.AcquireLock(L){ while (Fetch_And_Add(L,1)) L = 1; } ReleaseLock(L){ L = 0; }This implementation

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) fails as L can take on a non-zero value when the lock is actually available

Concurrency and Synchronization

Consider two processes, P1P_1 and P2P_2, attempting synchronization:

  1. Initially, L=0L = 0.

  2. P1P_1 executes AcquireLock(L): Fetch_And_Add(L, 1) reads 00 and sets L=1L = 1. Since the return value is 00, P1P_1 exits the while loop and enters the critical section.

  3. While P1P_1 is inside the critical section, P2P_2 calls AcquireLock(L): Fetch_And_Add(L, 1) reads 11 and sets L=2L = 2. Since the return value is non-zero, P2P_2 enters the body of the while loop, preparing to execute L = 1;.

  4. Now, P1P_1 finishes the critical section and executes ReleaseLock(L), setting L=0L = 0 (the lock is now actually available).

  5. Immediately after, P2P_2 executes the statement inside its while loop: L = 1;. This overwrites LL to 11 even though no process is holding the lock.

  6. On the next iteration of the while loop, P2P_2 calls Fetch_And_Add(L, 1), reads 11, and remains trapped in the loop. Consequently, no process can acquire the lock even though it was released.

Hence, the implementation fails because LL can take on a non-zero value when the lock is actually available (Option B).

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 Q32 of the GATE CSE 2012 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 2012.

How GATE tests Concurrency and Synchronization

More from GATE CSE 2012

Share this solution

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

GateAI solution cardGATE CSE 2012 · Q32 · MCQ

FetchAndAdd(X, i) is an atomic Read-Modify-Write instruction that reads the value of memory location X, increments it by the value i, and returns the old value of X. It is used in the pseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variable initialized to 0. The value of 0 corresponds to lock being available, while any non-zero value corresponds to the lock being not available. AcquireLock(L){ while (FetchAndAdd(L,1)) L = 1; } ReleaseLock(L){ L = 0; } This implementation

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.