GATE / CS ARCHIVE

GATE CSE 2006 / QUESTION 78

Reusable barrier generation race

Operating Systems · Synchronization & Concurrency · Full GATE CSE 2006 paper

GATE 2006, Q78 2 marks · MCQ
Report issue

Synchronization & Concurrency

Question 78, GATE 2006: 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. Why is the implementation incorrect?
Options:
(A) The use of binary semaphore S makes it wrong
(B) Two consecutive barrier invocations can deadlock
(C) Lines 6–10 do not need a critical section
(D) It is correct with two processes instead of three
Question 78, GATE 2006: 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. Why is the implementation incorrect?
Options:
(A) The use of binary semaphore S makes it wrong
(B) Two consecutive barrier invocations can deadlock
(C) Lines 6–10 do not need a critical section
(D) It is correct with two processes instead of three
Question 78, GATE 2006: 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. Why is the implementation incorrect?
Options:
(A) The use of binary semaphore S makes it wrong
(B) Two consecutive barrier invocations can deadlock
(C) Lines 6–10 do not need a critical section
(D) It is correct with two processes instead of three
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Once all three arrive, process_arrived=3. Let P leave the first barrier, increment process_left to 1, and immediately enter its next barrier before Q and R have finished the first.

2. It increments process_arrived to 4. If Q and R have not passed the first barrier’s while test, they now wait for process_arrived==3, and P also waits in its second invocation.

3. No one can perform the final reset: deadlock. Binary semaphores correctly protect the updates and are not the cause; the missing separation between barrier generations is.

4. The same issue can occur with two processes. Hence B.

5. Teaching check: The first invocation opens when arrival count reaches three, but the code does not close entry to the next generation. An early returning process can raise the count to four while another still tests for equality with three. A protected increment alone does not prevent mixing generations.

Original paper, page 12