GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 61

Atomic fetch-and-set binary semaphore

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2006 paper

GATE 2006, Q61 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 61, GATE 2006: Atomic fetch-and-set(x,y) sets shared location x to 1 and returns its old value in y, without an intervening access. Binary semaphore code is:
P(S): do { fetch-and-set(S.value,y); } while(y);
V(S): S.value=0;
Which statement is true?
Options:
(A) P may fail to work if context switching is disabled inside it
(B) A normal load/store pair can replace atomic fetch-and-set
(C) V is incorrectly implemented
(D) The code does not implement a binary semaphore
Question 61, GATE 2006: Atomic fetch-and-set(x,y) sets shared location x to 1 and returns its old value in y, without an intervening access. Binary semaphore code is:
P(S): do { fetch-and-set(S.value,y); } while(y);
V(S): S.value=0;
Which statement is true?
Options:
(A) P may fail to work if context switching is disabled inside it
(B) A normal load/store pair can replace atomic fetch-and-set
(C) V is incorrectly implemented
(D) The code does not implement a binary semaphore
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. P repeatedly atomically exchanges S.value with 1 and succeeds only when the previous value was 0. V releases it by storing 0.

2. On a uniprocessor, a process spinning in P with context switching disabled can prevent the lock owner from running V, so progress can fail. Separate load/store instructions are not atomic and allow two contenders to enter.

3. Thus A is true. This is a busy-wait semaphore using 0 for available and 1 for held; assume the intended atomic shared-memory model.

4. Teaching check: Atomicity prevents two contenders from both observing an available semaphore and entering. It does not guarantee progress if the spinning contender prevents the holder from being scheduled. Thus mutual-exclusion correctness and scheduling/liveness assumptions must be considered separately.

Original paper, page 9