Skip to content
Official PYQ · 2003

GATE CS 2003 Q81 — Concurrency and Synchronization

Explain with AI

Suppose we want to synchronize two concurrent processes PP and QQ using binary semaphores SS and TT. The code for the processes PP and QQ is shown below.

Process P:Process Q:  while(1) { while(1) {W:Y: print ‘0’; print ‘1’; print ‘0’; print ‘1’;X:Z:}}\begin{array}{|l|l|}\hline \text{Process P:} & \text{Process Q: } \\ \hline \text{ while(1) \{ } & \text{while(1) \{} \\ \text{W:} & \text{Y:} \\ \text{ print ‘0';} & \text{ print ‘1';} \\ \text{ print ‘0';} & \text{ print ‘1';} \\ \text{X:} & \text{Z:} \\ \text{\}} & \text{\}} \\ \hline \end{array}

Synchronization statements can be inserted only at points W,X,Y,W, X, Y, and ZZ

Which of the following will ensure that the output string never contains a substring of the form 01n001^n0 and 10n110^n1 where nn is odd?

Choose one option
View worked solution

Answer: C

To prevent the output string from containing substrings of the form 01n001^n0 and 10n110^n1 where nn is odd, we must ensure that any sequence of consecutive 11s (sandwiched between 00s) and any sequence of consecutive 00s (sandwiched between 11s) always has an even length.

Analyzing Option C:

  • Synchronization setup:
    • P(S)P(S) at WW and V(S)V(S) at XX
    • P(S)P(S) at YY and V(S)V(S) at ZZ
    • SS is a binary semaphore initialized to 11.
  • Effect: Since both processes compete for the same semaphore SS at the beginning of their loops (WW and YY) and release it only at the end (XX and ZZ), the critical sections are mutually exclusive.
  • This means once process PP starts executing, it cannot be interleaved by QQ until it finishes printing both 00s. Similarly, QQ cannot be interleaved by PP until it finishes printing both 11s.
  • Thus, the characters 00 and 11 are always printed in atomic blocks of "00" and "11" respectively.
  • Any output sequence will be a combination of these blocks, such as: "001111000011"\text{"001111000011"}
  • In any such sequence, the number of consecutive 11s or 00s is always a multiple of 22 (even). Hence, nn can never be odd, which satisfies the requirement.

Eliminating Other Options:

  • Options A, B, and D: These options use two different semaphores (SS and TT) initialized to 11. This allows both processes PP and QQ to enter their loops simultaneously. Their print statements can interleave, leading to sequences like 010010 (where n=1n = 1, which is odd).

Therefore, C is the correct option.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions