π Deadlocks and Starvation in Semaphore (36 MCQs)
Indefinite Waiting and Resource Deadlocks
Deadlock and starvation are synchronization problems that may occur when processes coordinate resource access through semaphores. Deadlock describes a situation in which processes remain permanently blocked because each waits for an event or resource that another blocked process must provide. Starvation occurs when a process waits indefinitely because competing processes repeatedly receive access or scheduling priority.
How Incorrect Semaphore Ordering Creates Circular Wait
Processes may acquire several resources in different orders, producing circular dependencies. If P1 holds resource A and waits for B while P2 holds B and waits for A, neither can proceed. Starvation can arise separately when a semaphore's wake-up policy repeatedly favours other waiting processes, even though the system continues making progress.
Semaphore Acquisition, Waiting Queues, and Release
Each process performs wait operations before using protected resources and signal operations after releasing them. Deadlock occurs when competing processes retain acquired resources while requesting additional unavailable resources. Starvation occurs when the waiting-queue policy fails to provide fair access. Consistent resource ordering, deadlock avoidance, and fair queue management address these different problems.
Two Semaphores Producing a Circular Wait
Suppose P1 acquires semaphore A and then waits for B, while P2 acquires B and then waits for A. Both semaphore permits are unavailable, so neither process can reach its signal operation. The processes remain blocked indefinitely. Requiring both processes to acquire A before B prevents this particular circular-wait pattern.
System Progress, Fairness, and Resource Utilisation
Deadlock can permanently reduce system throughput and leave allocated resources unavailable. Starvation can produce severe delays for individual processes even when other work completes. Semaphore-based systems require consistent resource ordering, suitable timeout or recovery policies where appropriate, and fair wake-up mechanisms. A fair queue can reduce starvation but does not independently eliminate every possible deadlock.
Misconceptions About Deadlock and Starvation
'Deadlock and starvation mean exactly the same thing' is incorrect because deadlock prevents participating processes from progressing, while starvation can occur amid system progress. 'Every waiting process is deadlocked' is incorrect because ordinary resource waiting may eventually end. 'Fair semaphore queues eliminate circular wait' is incorrect because queue fairness does not prevent resource dependency cycles.
Exam Focus: Detecting Circular Wait and Indefinite Postponement
Trace wait and signal sequences to identify held resources and blocked requests. Draw a resource-allocation graph and identify circular dependencies. Distinguish deadlock from starvation and ordinary blocking. Questions may test the four necessary deadlock conditions, resource ordering, fair semaphore queues, and strategies that prevent or reduce indefinite waiting.
Key Terms of Semaphore Deadlocks and Starvation
- Deadlock - Processes permanently waiting on one another.
- Starvation - Indefinite postponement of one process.
- Circular wait - Closed chain of resource dependencies.
- Resource ordering - Consistent sequence of resource acquisition.
- Fairness - Equitable opportunity for resource access.
- Recovery - Action restoring progress after deadlock.
π All Deadlocks and Starvation in Semaphore MCQs
Q1. In a semaphore-based program, P and Q are both blocked. Each is waiting for a signal that only the other blocked process can issue. What condition best describes this state?
π Explanation: Deadlock exists when every process in the set waits for an event that only another process in that same set can produce. Because both signal-producing actions are unreachable, neither process can progress.
Q2. P acquires semaphore S. Q waits while P performs finite work and is guaranteed to signal S before requesting anything else. Which conclusion is justified?
π Explanation: Waiting for an occupied semaphore is normal contention when its owner can finish and signal. Deadlock requires a dependency that prevents the needed signal from occurring, which this execution explicitly rules out.
Q3. Binary semaphores S and Q both start at 1. P0 executes wait(S) successfully; P1 executes wait(Q) successfully; P0 then blocks at wait(Q); P1 then blocks at wait(S). What is the resulting state?
π Explanation: P0 holds S and waits for Q, while P1 holds Q and waits for S. Each required signal depends on the other process progressing, so neither can release its held semaphore without external intervention.
Q4. A student labels every process blocked in wait(S) as deadlocked. Which correction identifies the flaw in this reasoning?
π Explanation: The flawed step equates waiting with deadlock. A semaphore owner may still be executing and later signal, so the analyst must establish a closed dependency preventing every relevant process from producing its required event.
Q5. A dependency graph has edges P β S1, S1 β Q, Q β S2, and S2 β P. Each semaphore has one permit, both processes are blocked, and neither can release its held permit independently. What does the graph show?
π Explanation: The edges show P requesting S1 held by Q and Q requesting S2 held by P. With one permit per semaphore and no independent release path, the cycle prevents either process from advancing.
Q6. Two processes are deadlocked because each holds a semaphore needed by the other. The scheduler is changed to round-robin with equal time slices. What should be expected?
π Explanation: Round-robin scheduling allocates CPU time among runnable processes, not blocked ones. Since each process needs a permit held by the other, changing the time-slice policy does not break the resource dependency.
Q7. Several processes may request semaphore A and semaphore B. Which rule most directly prevents the described deadlock pattern in which different processes acquire them in opposite orders?
π Explanation: A consistent global order prevents one process from holding a later resource while requesting an earlier one. Opposite acquisition orders can create circular wait; a shared order removes that possibility for these resources.
Q8. An allocation diagram for multiple-instance counting semaphores contains a cycle. One process in the diagram can finish finite work and signal its held semaphore without waiting for another permit. What conclusion is safest?
π Explanation: A cycle in a multiple-instance allocation diagram is not conclusive when a holder can release a permit independently. That signal may break the dependency and let another process continue, so execution details matter.
Q9. P holds semaphore S1 and waits for S2. Q holds S2 but can complete a finite independent operation and signal S2 without acquiring another semaphore. Is the pair necessarily deadlocked?
π Explanation: Q has an independent path to signal S2, so the event needed by P can still occur. Holding a resource while another process waits is not sufficient for deadlock when the holder can release it.
Q10. A blocked process is repeatedly denied semaphore S. An engineer raises its CPU priority and claims that this guarantees progress. What is wrong with the claim?
π Explanation: The flawed step treats scheduling priority as resource ownership. Priority affects which runnable process receives CPU time, but a blocked process still needs the semaphore holder to release a permit or the protocol to change.
Q11. A monitoring table records (checkpoint, completed jobs, cumulative wait of P in minutes): (1,4,5); (2,5,10); (3,6,15); (4,7,20); (5,8,25). Which interpretation best fits these data?
π Explanation: Completed jobs increase at every checkpoint, while Pβs cumulative wait grows steadily. This indicates overall system progress alongside indefinite postponement of one process, which is consistent with starvation rather than total deadlock.
Q12. A semaphore uses a FIFO wait queue. After being awakened, the oldest waiter is repeatedly preempted by higher-priority work and never runs long enough to complete its critical section. What remains possible?
π Explanation: FIFO controls which blocked waiter is selected when a permit is released. It does not guarantee CPU service after wake-up, so repeated preemption can still prevent that process from progressing and cause starvation.
Q13. A semaphore implementation removes waiters in LIFO order. New processes keep arriving, and the oldest waiter is never selected. Which problem can result?
π Explanation: LIFO selection favors the newest waiter. Under continuing arrivals, an older process can be bypassed repeatedly even while others acquire the semaphore, producing indefinite blocking or starvation despite continued system progress.
Q14. A system imposes the order S1 before S2 for every process. A process currently holds S2 and now needs S1. Which action follows the ordering policy?
π Explanation: The policy requires S1 to be acquired before S2. Holding S2 while requesting S1 violates the order and can recreate circular wait; releasing S2 lets the process restart acquisition in the permitted sequence.
Q15. A developer argues that a FIFO queue for every semaphore makes deadlock impossible. Which reasoning exposes the error?
π Explanation: The mistaken claim confuses fairness within one queue with the absence of resource cycles. FIFO may reduce starvation among waiters, but inconsistent acquisition orders can still leave processes holding permits needed by one another.
Q16. Which description best identifies starvation in semaphore use?
π Explanation: Starvation is process-specific indefinite postponement. Unlike deadlock, the system may continue completing work because other processes obtain the semaphore, while one unlucky waiter is repeatedly bypassed despite ongoing system progress elsewhere.
Q17. P holds semaphore S while waiting for a message. The only sender can send that message only after acquiring S. Which protocol change most directly removes this dependency?
π Explanation: P cannot receive the message while retaining S if the sender needs S to send it. Releasing S before the event wait, when safe for the shared data, removes the resource-dependent circular wait.
Q18. A dependency graph shows P holding A and requesting B, Q holding B and requesting C, and R holding C and requesting A. Each process keeps its held semaphore until the next request succeeds. What does the graph indicate?
π Explanation: P waits for Q, Q waits for R, and R waits for P through the held semaphores. Because none releases its current permit before its next request succeeds, the closed cycle prevents progress.
Q19. A high-priority process is blocked on S1. The process holding S1 is itself blocked on S2, which is held by a third process waiting for S1. Why does raising the first processβs priority not resolve the situation?
π Explanation: The dependency chain closes back on S1, so each process needs another holder to act first. CPU priority only helps runnable processes; it cannot grant a blocked process a permit held within the cycle.
Q20. Counting semaphore S starts at 2. P and Q each acquire one permit, then perform finite computations that require no further semaphore. Both will signal S afterward. Which conclusion is correct?
π Explanation: Both holders can complete without acquiring anything else, so each can eventually signal the semaphore. A zero available count indicates current occupancy, not deadlock, unless required progress depends on unavailable permits.
Q21. A student sees all permits of a counting semaphore in use and concludes that the system is deadlocked. Which missing check is essential?
π Explanation: The flawed inference treats complete occupancy as proof of deadlock. The analyst must determine whether permit holders can continue and signal; if they can, the current wait is temporary contention rather than deadlock.
Q22. A FIFO semaphore queue contains waiting processes in this order: P, then Q, then R. Two separate signal operations each wake one waiter, and no new process arrives. Who remains waiting afterward?
π Explanation: FIFO removes the oldest waiter first. The first signal wakes P and the second wakes Q, leaving R at the queue head; this ordering reduces the chance that an older waiter is repeatedly bypassed.
Q23. A deadlock has already been detected. The operating system terminates one process in the cycle and safely releases the semaphores it owned so another process can continue. How is this action classified?
π Explanation: The system acts after deadlock detection and breaks the resource cycle by terminating a participant. Because the action responds to an existing deadlock rather than preventing its conditions, it is recovery.
Q24. At five checkpoints, observations for processes P, Q, and R are: P wait = (2,4,6,8,10) seconds; Q wait = (2,1,2,1,2); R wait = (1,2,1,2,1). At each checkpoint another process acquires the semaphore. Which process is the strongest starvation candidate?
π Explanation: Pβs waiting time increases at every observation, while Q and R repeatedly obtain opportunities to proceed. The pattern supports process-specific starvation for P, not a system-wide deadlock affecting every waiter.
Q25. A program enforces a global order on all semaphores, preventing processes from requesting them in reverse order. Each semaphore still selects waiters randomly. Which statement is accurate?
π Explanation: A global order addresses circular wait, one source of deadlock. It does not guarantee fair selection from each waiting queue, so random selection can repeatedly bypass a process and allow starvation.
Q26. Two processes need semaphores A and B together. The system grants both permits as one indivisible reservation or grants neither. Which deadlock condition is directly reduced by this design?
π Explanation: A process never keeps only A while waiting for B, because it obtains both or neither. This removes hold-and-wait for the specified pair, although other resources and unfair allocation can still cause problems.
Q27. After detecting a deadlock, an administrator releases every semaphore held by P but lets P continue as though it still owns those resources. What is the main flaw?
π Explanation: The recovery step revokes ownership without changing Pβs execution state. P may continue modifying shared data after another process acquires the permit, so safe recovery must coordinate termination, rollback, or explicit reacquisition.
Q28. A program adds timeouts to semaphore waits. A timeout can identify a prolonged wait, but the program does not release resources or restore shared state when it expires. Which statement is correct?
π Explanation: A timeout reports that a wait exceeded a chosen interval; it does not by itself undo resource ownership or repair partially updated data. Recovery logic must safely release, reacquire, roll back, or terminate as appropriate.
Q29. Which semaphore queue policy most directly reduces starvation caused by newer arrivals repeatedly bypassing older waiters, assuming awakened processes eventually receive CPU time?
π Explanation: FIFO selects waiters in arrival order, so a newly arriving process cannot continually jump ahead of an older queued process. The guarantee still assumes permit holders release resources and awakened processes receive CPU service.
Q30. P holds semaphore S and waits for event E. The only process able to produce E must first acquire S. Which protocol change most directly breaks the circular dependency, assuming shared data can be left consistent?
π Explanation: The current protocol makes the event producer wait for S while P waits for the event. Releasing S before the event wait allows the producer to proceed; P can reacquire the semaphore afterward.
Q31. A system orders semaphores A before B before C. P holds A and requests B; Q holds B and requests C; R holds C and requests A. To preserve the order and break this cycle, what must R do?
π Explanation: The request from R to acquire A while holding later-ordered C violates the global order. Releasing C and restarting with A before C removes that reverse edge, breaking the circular-wait pattern.
Q32. P and Q form a closed semaphore dependency cycle and cannot proceed, while unrelated process R continues completing jobs. Which interpretation is most accurate?
π Explanation: Deadlock can affect a subset of processes rather than every process in the system. Rβs progress does not break the closed dependency between P and Q, whose required semaphore events remain unreachable.
Q33. A thread acquires a non-reentrant semaphore S and, before signaling it, calls wait(S) again. No other thread can signal S on its behalf. What is the likely result?
π Explanation: The thread already owns the only permit and blocks on its own second wait. Because it cannot execute the later signal while blocked, the event needed to resume itself cannot occur.
Q34. P holds A and needs B; Q holds B and needs A. The program is redesigned so a process that cannot obtain its next semaphore releases every semaphore it currently holds before retrying. Which effect is intended?
π Explanation: A process no longer keeps one semaphore while waiting for another unavailable semaphore. Releasing held permits before retrying removes hold-and-wait in this protocol and prevents the stated two-resource cycle, avoiding partial ownership during the wait.
Q35. A semaphore queue is fair, but its current owner waits indefinitely for a user event while still holding the permit. Other processes cannot acquire it. What does queue fairness fail to guarantee?
π Explanation: Fair selection applies only after a permit becomes available. It cannot force the current owner to signal, so indefinite resource holding can block every queued process and may participate in deadlock.
Q36. P holds X and waits for Y, while Q holds Y and waits for X. Neither needs the CPU for further computation before its second wait succeeds. Which redesign most directly removes the cycle without relying on scheduling?
π Explanation: The cycle persists because each process retains one permit while waiting for the other. Atomic acquisition or releasing held permits before waiting removes that partial ownership, breaking the described hold-and-wait cycle.