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 16–20 of 41
Consider the procedure below for the Producer-Consumer problem which uses semaphores:
Which one of the following is TRUE?
semaphore n = 0;
semaphore s = 1;
void producer() void consumer()
{ {
while(true) while(true)
{ {
produce(); semWait(s);
semWait(s); semWait(n);
addToBuffer(); removeFromBuffer();
semSignal(s); semSignal(s);
semSignal(n); consume();
} }
} }Which one of the following is TRUE?
Answer: (C)
In the consumer, semWait(s) comes before semWait(n).
Say the buffer is empty (n = 0) and the consumer gets s first. The consumer then waits on n and still holds s. The producer now waits on semWait(s), so it can never signal n. Deadlock.
Answer: Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty
Say the buffer is empty (n = 0) and the consumer gets s first. The consumer then waits on n and still holds s. The producer now waits on semWait(s), so it can never signal n. Deadlock.
Answer: Deadlock occurs if the consumer succeeds in acquiring semaphore s when the buffer is empty
A certain computation generates two arrays a and b such that a[i]=f(i)for and b[i] = g (a[i] )for . Suppose this computation is decomposed into two concurrent processes X and Y such that X computes the array a and Y computes the array b. The processes employ two binary semaphores R and S, both initialized to zero. The array a is shared by the two processes. The structures of the processes are shown below.
Which one of the following represents the CORRECT implementations of ExitX and EntryY?
Process X; Process Y;
private i; private i;
for (i = 0; i < n; i ++) { for (i = 0; i < n; i ++) {
a[i] = f(i); EntryY(R, S);
ExitX(R, S); b[i] = g(a[i]);
} } Which one of the following represents the CORRECT implementations of ExitX and EntryY?
Answer: (C)
Y must read a[i] only after X has written it, and R and S are binary semaphores that start at 0.
Option (C): ExitX is P(S); V(R), and EntryY is V(S); P(R). Y signals S to say it is ready, and X waits on S. After a[i] is written, X signals R, and Y waits on R. So Y reads a[i] only after X wrote it. X can never signal R twice before Y consumes it, because X's next V(R) needs Y's next V(S), and Y does that only after it passed P(R). So no signal is lost and there is no deadlock.
Why the others fail:
- (A): X does P(R) first and Y does P(S) first. Both semaphores are 0, so both block forever (deadlock).
- (B): ExitX is V(R); V(S) and EntryY is P(R); P(S). X can run ahead and do V(R) while R is already 1. A binary semaphore stays at 1, so that signal is lost. Y then waits forever for it.
- (D): ExitX is V(R); P(S). X can again do V(R) on R = 1, for example when Y is preempted just after V(S). One signal is lost and Y blocks forever on its last P(R).
Answer: (C)
Option (C): ExitX is P(S); V(R), and EntryY is V(S); P(R). Y signals S to say it is ready, and X waits on S. After a[i] is written, X signals R, and Y waits on R. So Y reads a[i] only after X wrote it. X can never signal R twice before Y consumes it, because X's next V(R) needs Y's next V(S), and Y does that only after it passed P(R). So no signal is lost and there is no deadlock.
Why the others fail:
- (A): X does P(R) first and Y does P(S) first. Both semaphores are 0, so both block forever (deadlock).
- (B): ExitX is V(R); V(S) and EntryY is P(R); P(S). X can run ahead and do V(R) while R is already 1. A binary semaphore stays at 1, so that signal is lost. Y then waits forever for it.
- (D): ExitX is V(R); P(S). X can again do V(R) on R = 1, for example when Y is preempted just after V(S). One signal is lost and Y blocks forever on its last P(R).
Answer: (C)
A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?
Answer: (D)
Since S = 2, two processes can be inside the critical section together. To get the largest x, the decrements must be lost:
- W reads x = 0 and waits there.
- Y runs to the end, so x = 2. Then Z runs to the end, so x = 4.
- W now stores 0 + 1 = 1, which overwrites 4.
- X then runs, and x = 2.
Answer: 2
- W reads x = 0 and waits there.
- Y runs to the end, so x = 2. Then Z runs to the end, so x = 4.
- W now stores 0 + 1 = 1, which overwrites 4.
- X then runs, and x = 2.
Answer: 2
Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the
P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlock-free order of invoking the P operations by the processes?
Answer: (B)
A deadlock cannot happen when every process asks for the semaphores in the same global order, because then a circular wait cannot form.
(B): X takes b, a, c. Y takes b, c, d. Z takes a, c, d. All of them follow the order b, a, c, d, so there is no deadlock.
The others can deadlock:
- (A): X takes a, Y takes b, Z takes c and d. Then X waits for b (held by Y), Y waits for c (held by Z), and Z waits for a (held by X). Circular wait.
- (C): X takes b, Y takes c, X takes a. Then X waits for c (held by Y) and Y waits for b (held by X).
- (D): X takes a and b, Y takes c. Then X waits for c (held by Y) and Y waits for b (held by X).
Answer: (B)
(B): X takes b, a, c. Y takes b, c, d. Z takes a, c, d. All of them follow the order b, a, c, d, so there is no deadlock.
The others can deadlock:
- (A): X takes a, Y takes b, Z takes c and d. Then X waits for b (held by Y), Y waits for c (held by Z), and Z waits for a (held by X). Circular wait.
- (C): X takes b, Y takes c, X takes a. Then X waits for c (held by Y) and Y waits for b (held by X).
- (D): X takes a and b, Y takes c. Then X waits for c (held by Y) and Y waits for b (held by X).
Answer: (B)
Fetch_And_Add(X,i) is an atomic Read-Modify-Write instruction that reads the value of memory location X, increments it by the value i, and returns the old value of X. It is used in the pseudocode shown below to implement a busy-wait lock. L is an unsigned integer shared variable initialized to 0. The value of 0 corresponds to lock being available, while any non-zero value corresponds to the lock being not available.
AcquireLock(L){
while (Fetch_And_Add(L,1))
L = 1;
}
ReleaseLock(L){
L = 0;
} This implementationAnswer: (B)
Say P1 holds the lock, so L = 1. P2 runs Fetch_And_Add and gets 1 back, so L becomes 2. Before P2 runs "L = 1", P1 releases the lock and L = 0. After that, P2 sets L = 1.
Now no process holds the lock, but L = 1. So every process keeps spinning forever.
Answer: fails as L can take on a non-zero value when the lock is actually available
Now no process holds the lock, but L = 1. So every process keeps spinning forever.
Answer: fails as L can take on a non-zero value when the lock is actually available
Keep practising
Topics in Operating System
System CallProcessThread
Process SynchronizationYou are here
DeadlockCPU SchedulingDisk SchedulingMemory ManagementFile Systems