Skip to content
Official PYQ · 2009

GATE CS 2009 Q33 — Concurrency and Synchronization

Explain with AI

The enter_CS() and leave_CS() functions to implement critical section of a process are realized using test-and-set instruction as follows:

void enter_CS(X)
{
    while(test-and-set(X));
}

void leave_CS(X)
{
    X = 0;
}

In the above solution, XX is a memory location associated with the CSCS and is initialized to 00. Now consider the following statements:

  1. The above solution to CSCS problem is deadlock-free

  2. The solution is starvation free

  3. The processes enter CSCS in FIFO order

  4. More than one process can enter CSCS at the same time

Which of the above statements are TRUE?

Choose one option
View worked solution

Answer: A

The test-and-set(X) instruction is an atomic hardware instruction that performs the following operations atomically:


temp = X;
X = 1;
return temp;

Initially, X=0X = 0. Let us evaluate each statement:

  1. Statement I: The solution is deadlock-free. (TRUE)
    If multiple processes attempt to enter the critical section simultaneously when X=0X = 0, exactly one process will successfully execute test-and-set(X), receive a return value of 00, and exit the while loop to enter the critical section. The other processes will spin in the loop. Since progress is guaranteed and there is no circular dependency preventing all processes from proceeding, the solution is deadlock-free.
  2. Statement II: The solution is starvation-free. (FALSE)
    There is no mechanism to guarantee bounded waiting. A process waiting in the while loop might repeatedly lose the race to other processes that exit and re-enter the critical section. Thus, a process can starve indefinitely.
  3. Statement III: The processes enter CS in FIFO order. (FALSE)
    The selection of the next process to enter the critical section is arbitrary and depends purely on CPU scheduling and which process executes the atomic test-and-set instruction first when XX becomes 00. No FIFO queue is maintained.
  4. Statement IV: More than one process can enter CS at the same time. (FALSE)
    Since test-and-set is atomic, only one process can read X=0X = 0 and set it to 11. Any subsequent process will read X=1X = 1 and remain blocked in the while loop. Thus, mutual exclusion is strictly preserved.

Therefore, only statement (I) is TRUE.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions