Skip to content
Official PYQ · 2007

GATE CS 2007 Q58 — Concurrency and Synchronization

Explain with AI

Two processes, P1P1 and P2P2, 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, wants11 and wants22 are shared variables, which are initialized to false.

Which one of the following statements is TRUE about the construct?

Choose one option
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 P1P_1 is in its CS. This implies P1P_1 set wants1 = true and successfully passed the loop while (wants2 == true); (meaning wants2 was false at the time of checking).
  • While P1P_1 is in its CS, wants1 remains true.
  • If P2P_2 now tries to enter its CS, it sets wants2 = true and reaches while (wants1 == true);. Since wants1 is true, P2P_2 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:

  1. P1P_1 executes wants1 = true;
  2. Context switch to P2P_2
  3. P2P_2 executes wants2 = true;
  4. P1P_1 resumes and executes while (wants2 == true);. Since wants2 is true, P1P_1 loops infinitely.
  5. P2P_2 resumes and executes while (wants1 == true);. Since wants1 is true, P2P_2 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.

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions