Skip to content
Official PYQ · 2015

GATE CS 2015 Set 3 Q10 — Concurrency and Synchronization

Explain with AI

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?

Choose one option
View worked solution

Answer: A

To analyze the given synchronization construct, we test for Mutual Exclusion and Deadlock.

1. Mutual Exclusion:
Mutual exclusion is violated if both processes can enter the Critical Section (CS) at the same time. Let's trace the execution:

  1. Initially, varP = false and varQ = false.
  2. Process XX executes varP = true;
  3. Process YY executes varQ = true;
  4. Now, both varP and varQ are true.
  5. Process XX checks while (varQ == true). Since varQ is true, XX enters its inner loop and accesses the CS.
  6. Process YY checks while (varP == true). Since varP is true, YY also enters its inner loop and accesses the CS.

Since both processes are in the Critical Section simultaneously, mutual exclusion is not guaranteed.

2. Deadlock:
Deadlock occurs if both processes are indefinitely blocked and cannot make any progress. In this construct, if a process does not enter its inner loop (because the condition is false), it simply loops back in the outer while(true) loop, which is non-blocking. If a process does enter the inner loop, it executes the CS and sets its control variable to false (e.g., varP = false), which immediately allows the other process to exit its loop. Thus, there is no scenario where both processes are permanently blocked. Hence, deadlock is prevented.

Therefore, the proposed solution prevents deadlock but fails to guarantee mutual exclusion.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions