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?
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:
- Initially,
varP = falseandvarQ = false. - Process executes
varP = true; - Process executes
varQ = true; - Now, both
varPandvarQaretrue. - Process checks
while (varQ == true). SincevarQistrue, enters its inner loop and accesses the CS. - Process checks
while (varP == true). SincevarPistrue, 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.
