Skip to content
Official PYQ · 2010

GATE CS 2010 Q45 — Concurrency and Synchronization

Explain with AI

The following program consists of 33 concurrent processes and 33 binary semaphores. The semaphores are initialized as S0=1,S1=0S0=1, S1=0 and S2=0.S2=0.
Process P0Process P1Process P2while (true) {wait (S1);wait (S2); wait (S0);release (S0);release (S0); print ‘0’; release (S1); release (S2);}\begin{array}{|l|l|}\hline \text{Process P0} & \text{Process P1} & \text{Process P2} \\ \hline \text{while (true) \{} & \text{wait (S1);} & \text{wait (S2);} \\ \text{ wait (S0);} & \text{release (S0);} & \text{release (S0);} \\ \text{ print ‘0';} & & \\ \text{ release (S1);} & & \\ \text{ release (S2);} & \text{} & \text{} \\ \text{\}} & \text{} \\ \hline \end{array}
How many times will process P0P0 print '00'?

Choose one option
View worked solution

Answer: A

To determine how many times process P0P_0 prints '0', let us trace the execution of the concurrent processes with binary semaphores initialized to S0=1,S1=0,S2=0S_0 = 1, S_1 = 0, S_2 = 0.

Initial State: S0=1,S1=0,S2=0S_0 = 1, S_1 = 0, S_2 = 0.

  1. P0P_0 executes wait(S0), decrements S0S_0 to 00, prints '0' (1st print), and executes release(S1) and release(S2).
    Current state: S0=0,S1=1,S2=1S_0 = 0, S_1 = 1, S_2 = 1.

Since P1P_1 and P2P_2 are non-looping processes, they can each execute their release(S0) at most once. Depending on the scheduling order, we have two possible cases:

  • Case 1 (Interleaved execution - 3 prints):
    • P1P_1 runs: wait(S1) →S1=0\rightarrow S_1 = 0, then release(S0) →S0=1\rightarrow S_0 = 1.
    • P0P_0 runs: wait(S0) →S0=0\rightarrow S_0 = 0, prints '0' (2nd print), and sets S1=1,S2=1S_1 = 1, S_2 = 1.
    • P2P_2 runs: wait(S2) →S2=0\rightarrow S_2 = 0, then release(S0) →S0=1\rightarrow S_0 = 1.
    • P0P_0 runs: wait(S0) →S0=0\rightarrow S_0 = 0, prints '0' (3rd print), and sets S1=1,S2=1S_1 = 1, S_2 = 1.
    • P0P_0 loops and blocks on wait(S0). Since both P1P_1 and P2P_2 have terminated, P0P_0 is blocked forever. Total prints = 3.
  • Case 2 (Consecutive execution - 2 prints):
    • P1P_1 runs: wait(S1) →S1=0\rightarrow S_1 = 0, then release(S0) →S0=1\rightarrow S_0 = 1.
    • P2P_2 runs: wait(S2) →S2=0\rightarrow S_2 = 0, then release(S0). Since S0S_0 is a binary semaphore and already 11, it remains 11.
    • P0P_0 runs: wait(S0) →S0=0\rightarrow S_0 = 0, prints '0' (2nd print), and sets S1=1,S2=1S_1 = 1, S_2 = 1.
    • P0P_0 loops and blocks on wait(S0). Since both P1P_1 and P2P_2 have terminated, P0P_0 is blocked forever. Total prints = 2.

Since the minimum number of prints is 2 and the maximum is 3, process P0P_0 will print '0' at least twice.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions