Two processes 𝑋 and 𝑌 need to access a critical section. Consider the…
2015
Two processes 𝑋 and 𝑌 need to access a critical section. Consider the following synchronization construct used by both the processes

Here, varP and varQ are shared variables and both are initialized to false. Which one of the following statements is true?
- A.
The proposed solution prevents deadlock but fails to guarantee mutual exclusion
- B.
The proposed solution guarantees mutual exclusion but fails to prevent deadlock
- C.
The proposed solution guarantees mutual exclusion and prevents deadlock
- D.
The proposed solution fails to prevent deadlock and fails to guarantee mutual exclusion
Attempted by 216 students.
Show answer & explanation
Correct answer: A
Answer: The proposed solution prevents deadlock but fails to guarantee mutual exclusion.
Reasoning:
What the code does: Each process sets its own flag to true, then executes a loop whose condition depends on the other process's flag; the critical section body is located inside that loop and the process sets its flag to false inside that body.
Why mutual exclusion fails: If both processes set their flags to true at about the same time, each sees the other's flag as true and then executes the code labeled as the critical section. Therefore both can be in the critical section simultaneously, so mutual exclusion is not guaranteed.
Concrete interleaving (counterexample):
Process X executes varP = true.
Almost concurrently, Process Y executes varQ = true.
Now X evaluates while(varQ == true) and enters the loop (critical section) because varQ is true.
Similarly, Y evaluates while(varP == true) and also enters its loop (critical section).
Why deadlock does not occur: Deadlock would require both processes to be indefinitely blocked waiting for the other to change a condition. In the shown code the condition check leads into the critical section rather than blocking until the other clears its flag, so processes proceed rather than becoming stuck waiting for the other.
Fix suggestion: To ensure mutual exclusion, use a known correct algorithm such as Peterson's algorithm (which uses two flags and a turn variable with proper ordering of checks), or use hardware-supported locks or higher-level synchronization primitives.
A video solution is available for this question — log in and enroll to watch it.