Skip to content
Official PYQ · 2020

GATE CS 2020 Q34 — Concurrency and Synchronization

Explain with AI

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

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

wait(a); count=count+1;
if (count==n) signal (b);
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?

Choose one option
View worked solution

Answer: A

This code implements a classic Barrier Synchronization mechanism. Let us analyze the execution step-by-step:

1. Initial State

  • Semaphore a=1a = 1 (used for mutual exclusion when updating the shared variable count).
  • Semaphore b=0b = 0 (used to block processes until all of them reach the barrier).
  • count = 0.

2. Execution of the First n−1n-1 Processes

When any of the first n−1n-1 processes finish CODE SECTION P and enter the synchronization block:

  1. They acquire the lock aa via wait(a).
  2. They increment count. Since count < n, the condition if (count == n) is false, so they do not signal bb.
  3. They release the lock aa via signal(a).
  4. They execute wait(b). Since b=0b = 0, these n−1n-1 processes get blocked here.

3. Arrival of the nn-th Process

When the nn-th process finishes CODE SECTION P and enters the synchronization block:

  1. It acquires lock aa, increments count to nn.
  2. Since count == n, it executes signal(b), which increments bb to 11.
  3. It releases lock aa via signal(a).
  4. It executes wait(b) (which succeeds, decrementing bb back to 00) and immediately executes signal(b) (incrementing bb back to 11).
  5. It then proceeds to CODE SECTION Q.

4. Cascading Wake-up

The signal(b) executed by the nn-th process wakes up one of the blocked processes waiting at wait(b). That awakened process then executes signal(b), which wakes up the next process, and so on. This cascading effect ensures all nn processes are unblocked and can proceed to CODE SECTION Q.

Conclusion

No process can enter CODE SECTION Q until the nn-th process has finished CODE SECTION P and signaled bb. Thus, it ensures that no process executes CODE SECTION Q before every process has finished CODE SECTION P.

Therefore, Option A is the correct choice.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions