Direction: Barrier is a synchronization construct where a set of processes synchronizes gobally, 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 3 and S be a binary semaphore with the usual P and V function. Consider the following C 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?
-
- The barrier implementation is wrong due to the use of binary semaphore S
- The barrier implementation may lead to a deadlock, if two barrier invocations are used in immediate succession
- Lines 6 to 10 need not be inside a critical section
- The barrier implementation is correct, if there are only two processes instead of three
- The barrier implementation is wrong due to the use of binary semaphore S
Correct Option: B
This barrier implementation is to keep track of arrival & completion of processes in system, by incrementing no. of process arrived & left. This implementation may lead to deadlock if two barrier function's invocations are used is immediate sessions. SinceV(s) in first invocation at line 3 removes 1-3 from critical section bring 5-11 in critical section at line 7. Hence (b) is correct option.