A Barrier is a synchronization construct where a set of processes synchronizes…

2006

A Barrier is a synchronization construct where a set of processes synchronizes globally. Each process arrives at the barrier and waits for all others to arrive; then all processes leave the barrier.

Given: Number of processes = 3.

  • S is a binary semaphore (initialized to 1).

  • Shared variables process_arrived and process_left are initialized to 0.

Code:

C

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);
}

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

  1. A.

    The barrier implementation is wrong due to the use of binary semaphore S.

  2. B.

    The barrier implementation may lead to a deadlock if two barrier invocations are used in immediate succession.

  3. C.

    Lines 6 to 10 need not be inside a critical section.

  4. D.

    The barrier implementation is correct if there are only two processes instead of three.

Attempted by 12 students.

Show answer & explanation

Correct answer: B

When the barrier is reused immediately, a process that has left the first barrier can enter the next barrier before the last process from the first barrier resets the counters. Its new arrival may be overwritten by the reset, so the processes in the next invocation can wait forever. Hence the implementation may deadlock for successive barrier invocations.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir