Deadlocks MCQs for NSCT
A deadlock is a state in which a set of processes are all blocked, each waiting for a resource held by another process in the same set, so none of them can ever continue. It is topic 7 of the Operating Systems area, which carries 5% of the NSCT according to HEC's Areas of Competencies. OS was the lowest-scoring area in 2026, with candidates averaging 33.8% accuracy, per the HEC NSCT report and APP.
Deadlock builds on concurrency and synchronisation: two mutex locks taken in opposite orders are the classic way to cause one. This page follows Silberschatz's Operating System Concepts.
Coffman conditions
Deadlock can happen only if all four of these conditions hold at the same time (the Coffman conditions):
| Condition | Meaning | Broken by |
|---|---|---|
| Mutual exclusion | At least one resource is non-sharable | Making resources sharable (rarely possible) |
| Hold and wait | A process holds resources while waiting for more | Requesting everything up front, or only when holding nothing |
| No pre-emption | Resources cannot be taken away | Letting the OS pre-empt resources from waiting processes |
| Circular wait | P0 waits for P1, …, Pn waits for P0 | Numbering resources and requesting them in increasing order |
A resource-allocation graph (RAG) shows processes, resources, request edges (P → R) and assignment edges (R → P). If every resource has one instance, a cycle means deadlock. With several instances, a cycle is necessary but not sufficient: a process outside the cycle may release an instance and break it.
Exam angle: "a cycle always means deadlock" is true only for single-instance resources. This is the trap most often set on RAG questions.
Banker's algorithm
Deadlock avoidance lets the OS grant a request only if the system stays in a safe state, one where some order exists in which every process can get its maximum claim and finish. The Banker's algorithm uses four structures for n processes and m resource types:
- Available: free instances of each type.
- Max: the most each process may ever request.
- Allocation: what each process holds now.
- Need = Max − Allocation.
The safety algorithm sets Work = Available, then repeatedly finds an unfinished process with Need ≤ Work, pretends it finishes, and adds its Allocation to Work. If all processes finish, the state is safe. It costs O(m × n²).
Example with Available (3, 1, 1):
| Process | Allocation | Max | Need |
|---|---|---|---|
| P0 | 1 0 2 | 4 3 3 | 3 3 1 |
| P1 | 2 1 0 | 3 2 2 | 1 1 2 |
| P2 | 0 2 1 | 5 2 4 | 5 0 3 |
| P3 | 1 1 1 | 2 2 2 | 1 1 1 |
| P4 | 0 0 2 | 3 3 3 | 3 3 1 |
If you always pick the lowest-numbered process that fits, you get P3 → (4,2,2), P1 → (6,3,2), P0 → (7,3,4), P2 → (7,5,5), P4 → (7,5,7). The state is safe, with the sequence P3, P1, P0, P2, P4.
Exam angle: safe sequences are often not unique, so check every option rather than looking for "the" sequence. An unsafe state is not yet a deadlock; it only means the OS can no longer guarantee that every process finishes. The Banker's algorithm page has more worked numericals.
Prevention, detection and recovery
Prevention makes one Coffman condition impossible by design. Requesting all resources at the start denies hold and wait, but wastes resources and can starve processes. Total resource ordering denies circular wait, and it is the method most used in practice for locks.
Detection lets deadlocks happen and then finds them. For single-instance resources, look for a cycle in the wait-for graph (the RAG with resource nodes removed). For several instances, run a Banker-like algorithm using each process's current Request instead of Need.
Recovery either aborts processes (all of them, or one at a time until the cycle breaks) or pre-empts resources. Pre-emption raises three issues: picking a victim, rolling it back, and avoiding starvation, usually by counting rollbacks in the victim's cost.
Silberschatz notes that most operating systems, including Linux and Windows, simply ignore deadlocks in application code (the "ostrich" approach) and leave it to developers.
| Strategy | When it acts | Cost |
|---|---|---|
| Prevention | Design time | Low utilisation |
| Avoidance (Banker's) | Each request | Needs Max in advance, O(m × n²) per check |
| Detection and recovery | After deadlock | Lost work when processes are aborted |
Exam angle: for n processes that each need at most k units of one resource, deadlock is impossible when the total is at least n(k − 1) + 1.
Common traps
- Treating a RAG cycle as proof of deadlock when resources have several instances.
- Calling an unsafe state "deadlocked".
- Computing Need as Allocation − Max, or forgetting to compute Available from the totals.
- Mixing up the conditions: resource ordering breaks circular wait, while "request everything first" breaks hold and wait.
- Checking a request against Available before checking it against Need. A request above Need is an error, not a wait.
- Confusing starvation (one process never served) with deadlock (a set stuck in a cycle).
Worked examples
Understanding. A policy lets the OS take resources away from a process that is waiting for something else. Which condition does it remove? Answer: no pre-emption. If held resources can be pre-empted, a waiting process cannot block others indefinitely. This works for resources whose state can be saved, such as CPU registers and memory, but not for printers or mutex locks.
Application. Four processes share a resource, and each needs at most 3 units. What is the fewest units that rules out deadlock? Answer: 9. In the worst case each process holds 2 units (8 in total) and waits for a third. One more unit lets a process finish and release its units, so 4 × 2 + 1 = 9.
Analysis. The state has Available (1, 1). Allocation is P0 (1, 0), P1 (1, 1), P2 (0, 1), and Max is P0 (3, 2), P1 (2, 2), P2 (2, 3). P2 requests (0, 1). Should it be granted? Answer: no, P2 must wait. The current state is safe: Need is P0 (2,2), P1 (1,1), P2 (2,2), and the sequence P1, P0, P2 works. Granting the request leaves Available at (1, 0), so P1's Need (1, 1) no longer fits, and neither does anyone else's. That state is unsafe, so the Banker's algorithm makes P2 wait.
Practise
The quiz below has 100 Deadlocks questions, which you can filter by tier and difficulty.
Practise: 100 questions
FAQs
What is the difference between deadlock prevention and avoidance?
Prevention makes deadlock impossible by design: it rules out one of the four conditions for every request, for example through resource ordering. Avoidance allows all four conditions but checks each request at run time, granting it only if the system stays in a safe state. Avoidance uses resources better but needs each process's maximum claim in advance.
Does a safe state guarantee there will be no deadlock?
Yes, as long as the OS keeps using the avoidance check. From a safe state there is at least one order in which every process can get its maximum and finish. The Banker's algorithm only grants requests that keep the system safe, so it never enters a state from which deadlock is possible.
Can deadlock happen with only one process?
Not in the usual sense, because the definition needs a set of processes waiting on each other. However, a single thread can block itself forever, for example by trying to lock a non-recursive mutex it already holds. Some texts call this self-deadlock. It is a programming bug, not a Coffman-style cycle between processes.
Is livelock the same as deadlock?
No. In livelock, processes are not blocked: they keep running and changing state, but they make no progress. An example is two threads that each back off and retry at the same moment, forever. Deadlocked processes are blocked and do nothing. Random back-off times usually fix livelock.
Next step
Continue with Memory Management MCQs, go back to the Operating Systems hub, or try a timed NSCT mock test.