Skip to content
Official PYQ · 2006

GATE CS 2006 Q78 — 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.

The above implementation of barrier is incorrect. Which one of the following is true?

Choose one option
View worked solution

Answer: B

To understand why the barrier implementation is incorrect, let us trace a scenario with three processes P1,P2,P_1, P_2, and P3P_3 where two barrier invocations occur in immediate succession:

  1. First Invocation: All three processes call barrier() and execute lines 1–3. The shared variable process_arrived becomes 33.
  2. Passing the Loop: Since process_arrived == 3, all three processes pass the busy-wait loop at line 4.
  3. Fast Process Execution: Suppose P1P_1 is very fast. It executes lines 5–11, incrementing process_left to 11, and exits the first barrier.
  4. Immediate Second Invocation: P1P_1 immediately calls barrier() a second time. It executes lines 1–3, incrementing process_arrived from 33 to 44, and releases the semaphore SS.
  5. Stuck at Loop: P1P_1 reaches line 4 of its second invocation: while (process_arrived != 3);. Since process_arrived is 44, P1P_1 gets stuck in this loop.
  6. Reset by Others: Meanwhile, P2P_2 and P3P_3 complete their first barrier invocation. When the last process (P3P_3) executes lines 7–10, it resets both process_arrived and process_left to 00.
  7. Deadlock: Now, P2P_2 and P3P_3 enter their second barrier invocation. They increment process_arrived to 11 and 22 respectively, and both get stuck at line 4. Since process_arrived is 22 and all three processes are trapped at line 4, the system is deadlocked.

Thus, the implementation can lead to a deadlock if two barrier invocations are used in immediate succession.

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

Published
Report a correction
Keep exploringMore Concurrency and Synchronization questions