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 36–40 of 41
The semaphore variables full, empty and mutex are initialized to 0, n and 1, respectively. Process P1 repeatedly adds one item at a time to a buffer of size n, and process P2 repeatedly removes one item at a time from the same buffer using the programs given below. In the programs, K, L, M and N are unspecified statements.
P1
P2
The statements K, L, M and N are respectively
P1
while (1) {
K;
P(mutex);
Add an item to the buffer;
V(mutex);
L;
} P2
while (1) {
M;
P(mutex);
Remove an item from the buffer;
V(mutex);
N;
} The statements K, L, M and N are respectively
Answer: (D)
empty counts free slots (starts at ). full counts items in the buffer (starts at 0).P1 is the producer. It needs a free slot, so it waits on
empty: K = P(empty). After adding an item there is one more item, so L = V(full).P2 is the consumer. It needs an item, so it waits on
full: M = P(full). After removing an item there is one more free slot, so N = V(empty).Other options: in (A) and (B) the producer waits on
full, which is 0 at the start, so it would block at once. In (C) the consumer waits on empty, which is , so it could remove an item from an empty buffer.Answer: (D)
Consider two processes P1 and P2 accessing the shared variables X and Y protected by two binary semaphores Sx and Sy respectively, both initialized to 1. P and V denote the usual semaphore operators, where P decrements the semaphore value, and V increments the semaphore value. The pseudo-code of P1 and P2 is as follows:
P1
P2
P1
While true do {
L1 : ................
L2 : ................
X = X + 1;
Y = Y - 1;
V(Sx);
V(Sy);
}P2
While true do {
L3 : ................
L4 : ................
Y = Y + 1;
X = Y - 1;
V(Sy);
V(Sx);
} In order to avoid deadlock, the correct operators at L1 , L2 , L3 and L4 are respectively.Answer: (D)
Deadlock needs a circular wait. It can be avoided if both processes take the two semaphores in the same order.
(D) P1 does
(A) P1 takes
(B) P1 takes
(C) P1 does
Answer: (D)
(D) P1 does
P(Sx), P(Sy) and P2 does P(Sx), P(Sy). Whichever process gets Sx first, the other blocks at its first P(Sx) and holds nothing. So the first process can take Sy, finish, and release both. Then the other goes on. No circular wait can form.(A) P1 takes
Sy then Sx, P2 takes Sx then Sy. Say P1 takes Sy and P2 takes Sx. Each now waits for the semaphore the other holds: deadlock.(B) P1 takes
Sx then Sy, P2 takes Sy then Sx. Opposite orders again: P1 holds Sx and wants Sy, P2 holds Sy and wants Sx: deadlock.(C) P1 does
P(Sx) twice. Sx is binary, so the second P(Sx) blocks P1 on a semaphore it already holds. P1 waits for ever. P(Sy) twice in P2 does the same.Answer: (D)
Suppose we want to synchronize two concurrent processes P and Q using binary semaphores S and T. The code for the processes P and Q is shown below.
Which of the following will ensure that the output string never contains a substring of the form or , where n is odd?
Process P:
while (1) {
W:
print '0';
print '0';
X:
}
Process Q:
while (1) {
Y:
print '1';
print '1';
Z:
} Synchronization statements can be inserted only at points W, X, Y and Z.Which of the following will ensure that the output string never contains a substring of the form or , where n is odd?
Answer: (C)
P prints 0 twice in a row and Q prints 1 twice in a row. If the two pairs never mix, the output is a string of whole blocks 00 and 11. Then every run of 1s between two 0s is made of whole 11 blocks, so its length n is even. The same holds for runs of 0s between two 1s. So the forbidden substrings and with odd n cannot appear.
To keep the pairs from mixing, the two critical sections (P's two prints, Q's two prints) must exclude each other. One semaphore S, starting at 1, with P(S) before each pair and V(S) after it, does this. That is option C.
Why the others fail (each lets the prints mix, for example 0 1 0, which has n = 1):
A: P uses S and Q uses T. These are different semaphores, so neither process ever waits for the other. P prints one 0, Q prints 1, P prints 0.
B: S and T both start at 1, so P(S) in P and P(T) in Q both succeed at once. Both processes enter together and their prints can interleave.
D: P does V(S) at W, which never blocks, so P prints without waiting. Q's P(S) at Y finds S = 1 and passes. So Q can print between P's two zeros.
Answer: (C)
To keep the pairs from mixing, the two critical sections (P's two prints, Q's two prints) must exclude each other. One semaphore S, starting at 1, with P(S) before each pair and V(S) after it, does this. That is option C.
Why the others fail (each lets the prints mix, for example 0 1 0, which has n = 1):
A: P uses S and Q uses T. These are different semaphores, so neither process ever waits for the other. P prints one 0, Q prints 1, P prints 0.
B: S and T both start at 1, so P(S) in P and P(T) in Q both succeed at once. Both processes enter together and their prints can interleave.
D: P does V(S) at W, which never blocks, so P prints without waiting. Q's P(S) at Y finds S = 1 and passes. So Q can print between P's two zeros.
Answer: (C)
Suppose we want to synchronize two concurrent processes P and Q using binary semaphores S and T. The code for the processes P and Q is shown below.
Which of the following will always lead to an output staring with '001100110011' ?
Process P:
while (1) {
W:
print '0';
print '0';
X:
}
Process Q:
while (1) {
Y:
print '1';
print '1';
Z:
} Synchronization statements can be inserted only at points W, X, Y and Z. Which of the following will always lead to an output staring with '001100110011' ?
Answer: (B)
For the output
P:
Trace: Q is blocked at
(A) uses
(C) has at the start, so Q can pass
(D) also keeps
Answer: (B)
001100110011..., P must print its two 0s, then Q its two 1s, and so on, taking turns with P first. Use for P's turn and for Q's turn.P:
P(S) at W, V(T) at X. Q: P(T) at Y, V(S) at Z. Start with and .Trace: Q is blocked at
P(T) because , so only P can start. P passes P(S) (), prints 00, and V(T) sets . P cannot go again, since . Q passes P(T), prints 11, and V(S) sets . Now P can go again. So the output is 00 11 00 11 and so on.(A) uses
S only in P and T only in Q. The two processes do not wait for each other, so the 0s and 1s can mix in any order.(C) has at the start, so Q can pass
P(T) and print 11 first. The output may start with 1.(D) also keeps
S in P and T in Q. Q waits on and nobody ever signals T before Q's own V(T), so Q never prints and the output is only 0s.Answer: (B)
Consider Peterson's algorithm for mutual exclusion between two concurrent processes i and j . The program executed by process is shown below.
repeat
flag [i] = true;
turn = j;
while ( P ) do no-op;
Enter critical section, perform actions, then exit critical
section
flag [ i ] = false;
Perform other non-critical section actions.
until false; For the program to guarantee mutual exclusion, the predicate P in the while loop should beAnswer: (B)
The paper says only "the program executed by process" without naming it; the code is that of process , as its use of
Process sets
Why this gives mutual exclusion. If both want to enter, both flags are true, and the shared variable
Why (A) fails. The loop would test
Why (C) and (D) fail. They test
Answer: (B)
flag[i] and turn = j shows.Process sets
flag[i] = true to show it wants to enter, then gives the turn to the other process with turn = j. It must wait while the other process also wants to enter and it is the other's turn. So P = flag[j] = true and turn = j.Why this gives mutual exclusion. If both want to enter, both flags are true, and the shared variable
turn holds the value written last. If turn = j, then waits and goes in (j's test is turn = i, which is false). If turn = i, then waits and goes in. Only one process can pass. So the process that writes turn first enters first, because the later write sends the turn back to the earlier process. If the other process is not interested, flag[j] is false and enters at once (progress). Since a process hands over the turn, it cannot enter again and again while the other waits (bounded waiting).Why (A) fails. The loop would test
turn = i, but has just set turn = j. So the test is false and enters even if is already inside its critical section, since left turn = i behind.Why (C) and (D) fail. They test
flag[i], which has just set to true, so they never look at whether the other process wants to enter. (D) is false straight away, so can enter while is inside. (C) makes wait even when is not interested.Answer: (B)
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems