Skip to main content

GATE CSE 2020Operating System › Process Synchronization & ConcurrencyVerified

GATE CSE 2020 PYQ: Each of a set of n n n processes executes the following code using two semaphores a a a and b b b…

Each of a set of nn processes executes the following code using two semaphores aa and bb initialized to 1 and 0, respectively. Assume that count\text{count} is a shared variable initialized to 0 and not used in CODE SECTION P.

CODE SECTION P\begin{array}{|c|} \hline \textbf{CODE SECTION P} \\ \hline \end{array}

  1. wait(a); count=count+1;
  2. if (count==n) signal (b);
  3. signal (a); wait (b) ; signal (b);

CODE SECTION Q\begin{array}{|c|} \hline \textbf{CODE SECTION Q} \\ \hline \end{array}

What does the code achieve?

  1. A
    It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.
    Correct answer
  2. B
    It ensures that atmost two processes are in CODE SECTION Q at any time.
  3. C
    It ensures that all processes execute CODE SECTION P mutually exclusively.
  4. D
    It ensures that at most n−1n-1 processes are in CODE SECTION P at any time.

Answer

(A) It ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.

Approach

Interpret the code as a barrier synchronization placed between CODE SECTION P and CODE SECTION Q. Semaphore a protects the shared counter count, and semaphore b is used to hold all processes until the last process has incremented count to n.

Semaphore a serializes updates to count and detects when all n processes have completed P. Semaphore b is initially 0, so all processes block after the count update until the last process signals b; afterward, each process signals b again to let the remaining processes proceed. This implements a barrier: no process enters CODE SECTION Q until every process has finished CODE SECTION P.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.