Skip to main content

GATE CSE 2015Operating System › Process Synchronization & ConcurrencyVerified

GATE CSE 2015 PYQ: Two processes X X X and Y Y Y need to access a critical section. Consider the following synchronization…

Two processes XX and YY need to access a critical section. Consider the following synchronization construct used by both the processesProcess XProcess Y/* other code for process X*//* other code for process Y */while (true)while (true){{varP = true;varQ = true;while (varQ == true)while (varP == true){{/* Critical Section *//* Critical Section */varP = false;varQ = false;}}}}/* other code for process X *//* other code for process Y */\begin{array}{|l|l|}\hline \text{Process X} & \text{Process Y} \\ \hline \text{/* other code for process X*/} & \text{/* other code for process Y */} \\ \text{while (true)} & \text{while (true)} \\ \text{\{} & \text{\{} \\ \quad\text{varP = true;} & \quad \text{varQ = true;} \\ \quad\text{while (varQ == true)} &\quad \text{while (varP == true)}\\ \quad\text{\{} & \quad\text{\{} \\ \quad\quad\text{/* Critical Section */} & \quad\quad\text{/* Critical Section */} \\ \quad\quad\text{varP = false;} &\quad\quad \text{varQ = false;} \\ \quad\text{\}} & \quad\text{\}} \\ \text{\}} & \text{\}} \\ \text{/* other code for process X */} & \text{/* other code for process Y */}\\ \\ \hline \end{array}

Here varP and varQ are shared variables and both are initialized to false. Which one of the following statements is true?

  1. A
    The proposed solution prevents deadlock but fails to guarantee mutual exclusion
    Correct answer
  2. B
    The proposed solution guarantees mutual exclusion but fails to prevent deadlock
  3. C
    The proposed solution guarantees mutual exclusion and prevents deadlock
  4. D
    The proposed solution fails to prevent deadlock and fails to guarantee mutual exclusion

Answer

(A) The proposed solution prevents deadlock but fails to guarantee mutual exclusion

Approach

Read the code literally as given (the semicolon placement matters), then test the two standard properties: (i) can both processes be inside the critical section at the same time, and (ii) can a process be blocked forever waiting for the other. Mutual exclusion is a necessary condition for deadlock, and here it is exactly the property that breaks first.

The flags are set before the other's flag is tested and there is no semicolon terminating the while condition, so the critical section lies inside the loop body. Both processes can therefore pass their tests simultaneously and execute the critical section together, breaking mutual exclusion. Deadlock cannot arise because mutual exclusion - a necessary condition for deadlock - is not satisfied, and in practice every process resets its own flag inside the body, so the other's loop condition eventually becomes false. Hence option (A).

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.