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; }
Fails because L can overflow
Fails because L can be nonzero while the lock is available
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
GATE 2013 CS (CS1) Q44– x initially zero is shared by W,X,Y,Z. Each process waits on counting semaphore S=2, reads…
GATE 2013 CS (CS1) Q49– X computes a[i]=f(i), then ExitX(R,S), for i=0,…,n−1. Y calls EntryY(R,S), then computes…
GATE 2010 CS (CS1) Q33– Shared Boolean S1,S2 start with arbitrary values. P1 waits while S1==S2, enters its…
GATE 2010 CS (CS1) Q55– Binary semaphores S0,S1,S2 start at 1,0,0. Concurrent processes execute: How many times is…
GATE 2015 CS (CS3) Q31– Shared varP,varQ start false. These loops deliberately put the critical sections INSIDE…
GATE 2026 CS (CS2) Q51– Consider three processes P 1, P 2, and P 3 running identical code shown below. A and B are…