The GATE Grind

GATE 2012 CS (CS1) – Question 42

Operating System · Concurrency and Synchronization · 2 marks · Multiple choice

Fetch_And_Add(L,i) atomically returns old L and increments L by i. Shared unsigned L initially zero means unlocked; nonzero means locked. Assess this implementation.

AcquireLock(L) {
    while (Fetch_And_Add(L,1))
        L=1;
}
ReleaseLock(L) { L=0; }
  1. Fails because L can overflow
  2. Fails because L can be nonzero while the lock is available
  3. Works but may starve processes
  4. Works without starvation

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (B) Fails because L can be nonzero while the lock is available

Explanation

A waiter can execute Fetch_And_Add and see nonzero, then pause before its loop-body assignment. The owner releases by setting L=0. The waiter resumes and writes L=1 even though nobody owns the lock. Future attempts see nonzero forever unless an owner releases, but none exists. Thus B: a stale write can lose the unlocked state.

More Concurrency and Synchronization questions

All Concurrency and Synchronization questions →