Process Synchronization · Operating System
41 questions · asked in 23 of 27 years (2000–2026)
MCQ 34 · MSQ 4 · NAT 31-mark 22 · 2-mark 19
2000Questions per year2026
Questions 31–35 of 41
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 S be a binary semaphore with the usual P and V functions. Consider the following C implementation of a barrier with line numbers shown on left.
Which one of the following rectifies the problem in the implementation?
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.Which one of the following rectifies the problem in the implementation?
Answer: (B)
The flaw.
(B) fixes it. If the first process of a new call waits until
(A) does not fix it. It removes the
(C) is wrong. A process spinning at line 4 with context switch disabled never lets the others run and arrive, so the first call already deadlocks.
(D) is wrong. A private
Answer: (B)
process_arrived is reset to 0 only by the last process to leave (process_left == 3). A fast process can leave, call barrier again, and run lines 1 to 3 while the other two have not left yet. It raises process_arrived to 4 and spins at line 4 for ever, because 4 is not 3. When the other two finally leave, process_left becomes 3 and both counters are reset to 0, which also wipes out that early arrival. The three processes can then reach at most 2 arrivals, so all of them spin at line 4: deadlock.(B) fixes it. If the first process of a new call waits until
process_arrived is 0, it cannot count itself in before the previous round has been reset. So process_arrived never goes above 3 and the reset is not lost.(A) does not fix it. It removes the
process_left count, so nothing makes a process wait until the others have left. A fast process can still come back and change process_arrived while the others are still at line 4, and the barrier is still broken.(C) is wrong. A process spinning at line 4 with context switch disabled never lets the others run and arrive, so the first call already deadlocks.
(D) is wrong. A private
process_left reaches only 1 in each process, never 3, so process_arrived is never reset. In the second call it goes past 3 and the processes wait for ever.Answer: (B)
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 S be a binary semaphore with the usual P and V functions. Consider the following C implementation of a barrier with line numbers shown on left.
The above implementation of barrier is incorrect. Which one of the following is true?
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?
Answer: (B)
(B) is true. Let
(A) is false.
(C) is false. Lines 6 to 10 change shared variables. Without
(D) is false. With two processes the same trace works: one process comes back early, raises
Answer: (B)
p1, p2, p3 reach the barrier. After line 2 has run for all three, process_arrived = 3 and the loop at line 4 lets them through. Say p1 runs the whole function: process_left = 1, and no reset yet. It calls barrier again, and at line 2 it makes process_arrived = 4. At line 4 it is stuck, since 4 is not 3. Now p2 and p3 finish the first call. process_left becomes 3, so both counters are reset to 0. They call barrier again, and process_arrived reaches only 2. No process can pass line 4. This is a deadlock.(A) is false.
S is used correctly: P(S) and V(S) wrap each update of the shared counters. The fault is in the logic of the counters, not in the semaphore being binary.(C) is false. Lines 6 to 10 change shared variables. Without
S, two processes could run process_left++ together and lose an update, so they must stay in a critical section.(D) is false. With two processes the same trace works: one process comes back early, raises
process_arrived past 2, and the reset leaves the counter too low for the barrier to open again.Answer: (B)
The atomic feth-and-set x,y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x . Consider the following implementation of P and V functions on a binary semaphore S.
void P(binary_semaphore*S){
unsigned y;
unsigned*x =& (S->value);
do {
fetch-and-set x,y;
} while(y);
}
void V (binary_semphore*S){
S_>value = 0;
} Which one of the following is true?Answer: (A)
fetch-and-set sets S->value to 1 and returns the old value in y. So P loops (busy-waits) until it reads 0 (semaphore free) and leaves it as 1 (taken). V sets it to 0.(A) is true. Take one processor. If context switching is disabled inside
P and the semaphore is already taken, the process spins in the loop and is never taken off the CPU. The process that holds the semaphore never runs, so it never calls V. The spinning goes on for ever.(B) is false. A normal load followed by a store is not atomic. Two processes could both load 0 and both store 1, and both would enter, so mutual exclusion breaks.
(C) is false. Setting
S->value = 0 in V is right, because 0 means free for the test in P.(D) is false. Only one process gets past
P until V is called, so this does work as a binary semaphore (with busy waiting).Answer: (A)
Two concurrent processes P1 and P2 use four shared resources R1, R2, R3 and R4, as shown below.
Both processes are started at the same time, and each resource can be accessed by only one process at a time The following scheduling constraints exist between the access of resources by the processes:
P2 must complete use of R1 before P1 gets access to R1.
P1 must complete use of R2 before P2 gets access to R2.
P2 must complete use of R3 before P1 gets access to R3.
P1 must complete use of R4 before P2 gets access to R4.
There are no other scheduling constraints between the processes. If only binary semaphores are used to enforce the above scheduling constraints, what is the minimum number of binary semaphores needed?
Both processes are started at the same time, and each resource can be accessed by only one process at a time The following scheduling constraints exist between the access of resources by the processes:
P2 must complete use of R1 before P1 gets access to R1.
P1 must complete use of R2 before P2 gets access to R2.
P2 must complete use of R3 before P1 gets access to R3.
P1 must complete use of R4 before P2 gets access to R4.
There are no other scheduling constraints between the processes. If only binary semaphores are used to enforce the above scheduling constraints, what is the minimum number of binary semaphores needed?
Answer: (B)
Each process uses R1, R2, R3, R4 in this order. The four rules fix who goes first on each resource: P2 on R1, P1 on R2, P2 on R3, P1 on R4. So the turn passes P2 to P1, then P1 to P2, then P2 to P1, then P1 to P2.
Use two semaphores, both starting at 0: means "P2 is done, P1 may go", and means "P1 is done, P2 may go".
P1: P(X); use R1; use R2; V(Y); P(X); use R3; use R4; V(Y)
P2: use R1; V(X); P(Y); use R2; use R3; V(X); P(Y); use R4
Why each rule holds:
R1: P1 waits on X, and P2 does V(X) right after R1.
R2: P2 waits on Y, and P1 does V(Y) right after R2.
R3: P1 waits on X again, and P2 does V(X) right after R3.
R4: P2 waits on Y again, and P1 does V(Y) right after R4.
Each V is done only after the earlier signal on that semaphore has been taken, so X and Y never need a value above 1. Binary semaphores are enough.
One semaphore is not enough. The signals go in both directions, and with a single semaphore the process that signals could take its own signal back with a later P. Then the other process would not be forced to go first. So each direction needs its own semaphore, which gives a minimum of 2.
Answer: (B)
Use two semaphores, both starting at 0: means "P2 is done, P1 may go", and means "P1 is done, P2 may go".
P1: P(X); use R1; use R2; V(Y); P(X); use R3; use R4; V(Y)
P2: use R1; V(X); P(Y); use R2; use R3; V(X); P(Y); use R4
Why each rule holds:
R1: P1 waits on X, and P2 does V(X) right after R1.
R2: P2 waits on Y, and P1 does V(Y) right after R2.
R3: P1 waits on X again, and P2 does V(X) right after R3.
R4: P2 waits on Y again, and P1 does V(Y) right after R4.
Each V is done only after the earlier signal on that semaphore has been taken, so X and Y never need a value above 1. Binary semaphores are enough.
One semaphore is not enough. The signals go in both directions, and with a single semaphore the process that signals could take its own signal back with a later P. Then the other process would not be forced to go first. So each direction needs its own semaphore, which gives a minimum of 2.
Answer: (B)
Given below is a program which when executed spawns two concurrent processes :
semaphore X : = 0 ;
/* Process now forks into concurrent processes P1 & P2 */
Consider the following statements about processes P1 and P2:
I.It is possible for process P1 to starve.
II.It is possible for process P2 to starve.
Which of the following holds?
semaphore X : = 0 ;
/* Process now forks into concurrent processes P1 & P2 */
Consider the following statements about processes P1 and P2:
I.It is possible for process P1 to starve.
II.It is possible for process P2 to starve.
Which of the following holds?
Answer: (A)
Take the classical busy-wait definition of the operations, with at the start. P(X): loop while , then do . V(X): do . A process starves when it keeps trying to pass P(X) but is overtaken every time.
P2 can starve (statement II). P2 passes P(X) only if it tests X while . Let P1 repeat V(X) (), Compute, P(X) (). Let P2 run only just after P1's P(X), when . P2 then sees 0 every time and keeps looping, while P1 goes on forever.
P1 can starve (statement I). P1 does V(X), so . P2 does P(X), so , and then Compute. P1 reaches P(X) and sees 0, so it loops. Now P2 does V(X), so , and goes straight to its next P(X), so again before P1 tests X. P1 only ever sees 0. This can repeat for ever.
Both statements are true.
Answer: (A)
P2 can starve (statement II). P2 passes P(X) only if it tests X while . Let P1 repeat V(X) (), Compute, P(X) (). Let P2 run only just after P1's P(X), when . P2 then sees 0 every time and keeps looping, while P1 goes on forever.
P1 can starve (statement I). P1 does V(X), so . P2 does P(X), so , and then Compute. P1 reaches P(X) and sees 0, so it loops. Now P2 does V(X), so , and goes straight to its next P(X), so again before P1 tests X. P1 only ever sees 0. This can repeat for ever.
Both statements are true.
Answer: (A)
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems