Three processes repeatedly synchronize at a barrier. Shared process_arrived and process_left start at 0. S is a binary semaphore, initially available; P acquires and V releases it. Each process calls:
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);
}
Use the intended shared-memory pseudocode model, not a claim of data-race-safe modern C. Which proposed repair addresses its reuse problem? The phrase “first process” in B is only a conceptual generation gate and needs a concrete synchronization implementation.
Options:
(A) Replace lines 6–10 by process_arrived--
(B) At barrier entry, the first process waits for process_arrived to become zero before executing P(S)
(C) Disable context switching throughout the barrier
(D) Make process_left private instead of shared
Answer & solution
Answer: B, with generation-entry qualification (no official key was published)
1. The intended repair is to prevent a process from incrementing the next generation’s arrival count until the previous generation has reset process_arrived to zero, corresponding to B. It must wait before acquiring S, leaving S available to the final departing process that performs the reset.
2. A lets an early departure lower process_arrived while others still need to observe 3; C prevents necessary scheduling on a uniprocessor; D prevents the shared departure count reaching 3. However the phrase first process to enter is a conceptual generation gate, not complete executable synchronization.
3. Merely adding an unconditional while(process_arrived!=0) to every invocation blocks the second arrival of the same generation; waiting only one vaguely designated process while allowing another new-generation entrant through is also insufficient. A concrete reusable barrier needs a correctly synchronized generation flag or two-turnstile design.
4. Teaching check: Waiting while holding S would prevent the final departing process from acquiring S to reset the counters. Waiting must therefore occur outside that critical section, and it must distinguish generations correctly. A two-turnstile or generation-counter barrier is a concrete way to enforce this separation.
Original paper, page 12