Skip to content
Official PYQ · 2006

GATE CS 2006 Q61 — Concurrency and Synchronization

Explain with AI

The atomic fetch-and-set x,yx, y instruction unconditionally sets the memory location xx to 11 and fetches the old value of xx in yy without allowing any intervening access to the memory location xx. Consider the following implementation of PP and VV functions on a binary semaphore SS.

void P (binary_semaphore *s) { 
    unsigned y; 
    unsigned *x = &(s->value); 
    do { 
        fetch-and-set x, y; 
    } while (y); 
}

void V (binary_semaphore *s) { 
    S->value = 0; 
} 

Which one of the following is true? 

Choose one option
View worked solution

Answer: A

To determine which statement is true, let us analyze the behavior of the fetch-and-set instruction and the implementation of the PP and VV functions.

1. Analysis of the Implementation

  • fetch-and-set x, y: This atomic instruction sets the memory location xx (which points to s->value) to 11 and stores its old value in yy.
  • Function P(s)P(s) (Locking):
    • If s->value is initially 00 (semaphore is free), fetch-and-set sets it to 11 and returns y=0y = 0. The loop condition while (y) becomes false, and the process successfully enters the critical section.
    • If s->value is initially 11 (semaphore is locked), fetch-and-set sets it to 11 (no change) and returns y=1y = 1. The loop condition while (y) remains true, causing the process to busy-wait (spin-lock).
  • Function V(s)V(s) (Unlocking): It unconditionally resets s->value to 00, allowing another waiting process to acquire the lock.

2. Evaluating the Options

  • Option A: If context switching is disabled while a process is executing PP, and the semaphore is already locked (s->value == 1), the process will enter the do-while loop and spin indefinitely. Since context switching is disabled, the CPU cannot switch to the process holding the lock to let it execute VV and release it. This leads to a permanent deadlock. Thus, the implementation may not work if context switching is disabled in PP. This statement is true.
  • Option B: Normal load/store operations are non-atomic. Using them instead of fetch-and-set would lead to race conditions, failing to guarantee mutual exclusion. Thus, this statement is false.
  • Option C & D: The code correctly implements a binary semaphore using a spin-lock mechanism, and the VV implementation is correct. Thus, these statements are false.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions