Two processes, and , need to access a critical section of code. Consider the following synchronization construct used by the processes:
/* P1 */
while (true) {
wants1 = true;
while (wants2 == true);
/* Critical Section */
wants1 = false;
}
/* Remainder section */
| /* P2 */
while (true) {
wants2 = true;
while (wants1 == true);
/* Critical Section */
wants2=false;
}
/* Remainder section */
|
Here, wants and wants are shared variables, which are initialized to false.
Which one of the following statements is TRUE about the construct?
View worked solution
Answer: D
To analyze the given synchronization construct, let us evaluate the two key properties: Mutual Exclusion and Deadlock Prevention.
1. Mutual Exclusion
For a process to enter its Critical Section (CS), it must set its own flag to true and then find the other process's flag to be false.
- Suppose is in its CS. This implies set
wants1 = trueand successfully passed the loopwhile (wants2 == true);(meaningwants2wasfalseat the time of checking). - While is in its CS,
wants1remainstrue. - If now tries to enter its CS, it sets
wants2 = trueand reacheswhile (wants1 == true);. Sincewants1istrue, will get stuck in this loop and cannot enter its CS.
Thus, both processes can never be in the critical section simultaneously. Mutual exclusion is guaranteed.
2. Deadlock
Let us consider the following interleaved execution sequence starting with wants1 = false and wants2 = false:
- executes
wants1 = true; - Context switch to
- executes
wants2 = true; - resumes and executes
while (wants2 == true);. Sincewants2istrue, loops infinitely. - resumes and executes
while (wants1 == true);. Sincewants1istrue, loops infinitely.
Both processes are now waiting for each other to set their respective flags to false, which will never happen. This is a state of deadlock.
Therefore, the construct does not prevent deadlocks, but ensures mutual exclusion.
Free practice · Your answers on this page are not saved.
