Skip to content
Official PYQ · 2006

GATE CS 2006 Q79 — Concurrency and Synchronization

Explain with AI

Barrier is a synchronization construct where a set of processes synchronizes globally i.e., each process in the set arrives at the barrier and waits for all others to arrive and then all processes leave the barrier. Let the number of processes in the set be three and SS be a binary semaphore with the usual PP and VV functions. Consider the following CC implementation of a barrier with line numbers shown on left.

void barrier (void) {

1 P(S);
2 process_arrived++;
3 V(S);
4 while (process_arrived !=3);
5    P(S);
6    process_left++;
7    if (process_left==3) {
8       process_arrived = 0;
9       process_left = 0;
10   }
11 V(S);

}

The variables process_arrived and process_left are shared among all processes and are initialized to zero. In a concurrent program all the three processes call the barrier function when they need to synchronize globally.

Which one of the following rectifies the problem in the implementation?

Choose one option
View worked solution

Answer: B

In the given implementation, a fast process (say, P3P_3) can exit the barrier and immediately call barrier() again for the next round of synchronization before the slowest process (say, P2P_2) has finished executing lines 8–10 to reset the shared variables.

If P3P_3 enters the barrier again while process_arrived is still 33:

  • It executes lines 1–3, incrementing process_arrived to 44.
  • It then gets stuck in the busy-wait loop at line 4 because process_arrived != 3 remains true forever.

To prevent this race condition, the first process attempting to enter the next round of the barrier must wait until the previous round has completely finished resetting the state. Specifically, it must wait until process_arrived becomes 00 before proceeding to execute P(S). This is precisely what Option B describes.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions