Deadlock
GATE CS85 min read
Deadlock and resource model
Definition. A deadlock is a state in which a set of processes is permanently blocked because each waits for an event that only another process in the set can cause. Resources may have one or multiple instances; a process requests, uses, and releases them.
Example. P1 holds printer A and requests scanner B while P2 holds B and requests A.
Exam point. A blocked process alone is not enough; the waiting dependency must form a cycle or equivalent circular dependency.
Solved examples
Example 1 — dependency cycle. P1 holds R1 and requests R2; P2 holds R2 and requests R1.
- P1 cannot proceed until R2 is released by P2.
- P2 cannot proceed until R1 is released by P1.
- Neither can release its held resource before proceeding.
- The two-process wait cycle is deadlocked.
Example 2 — no cycle. P1 holds R1 and waits for R2; P2 holds R2 but is not waiting and will release it.
- P1 is blocked temporarily.
- P2 can execute and release R2.
- P1 can then proceed.
- This is not a deadlock.
Four necessary conditions
Definition. Deadlock requires all four Coffman conditions: mutual exclusion, hold-and-wait, no preemption, and circular wait. Breaking any one condition prevents deadlock under the modeled resource assumptions.
| Condition | Meaning | Possible strategy to break it |
|---|---|---|
| Mutual exclusion | resource has nonshareable use | make resource shareable where feasible |
| Hold-and-wait | hold resources while requesting more | request all at once or release before request |
| No preemption | resources cannot be forcibly taken | preempt eligible resources |
| Circular wait | cyclic chain of requests | impose a global resource order |
Example. Requiring processes to request resources in increasing order prevents a cycle in that order.
Exam point. These are necessary conditions, not a sufficient test for every system state.
Solved examples
Example 1 — find the broken condition. System requires a process to request its complete resource set before it starts.
- The process cannot hold a partial set and later request more.
- Hold-and-wait is prevented.
- Deadlock is prevented through that condition, though utilization may fall.
Example 2 — ordered resources. Resources are ordered R1<R2<R3; all requests must follow increasing order.
- A cycle would require requests eventually to move back to a lower-ranked resource.
- Every edge in a wait cycle would have to increase rank strictly.
- A finite cycle cannot strictly increase and return to its start.
- Circular wait is impossible.
Resource-allocation graphs
Definition. In a resource-allocation graph, process nodes and resource nodes are connected by request edges P→R and assignment edges R→P. With one instance per resource type, a cycle is equivalent to deadlock. With multiple instances, a cycle is necessary but not sufficient.
Example. P1→R2 and R1→P1 show P1 waits for R2 while holding R1.
Exam point. Read edge direction carefully: request points from process to resource; assignment points from resource to process.
Solved examples
Example 1 — single-instance cycle. Edges R1→P1, P1→R2, R2→P2, P2→R1.
- P1 holds R1 and requests R2.
- P2 holds R2 and requests R1.
- The graph contains a cycle through both processes and resources.
- With one instance of each resource, the cycle implies deadlock.
Example 2 — multiple instances caution. Resource R has two identical instances; P1 requests R, P2 holds one instance but is not blocked and will release it.
- A graph cycle may include requests and assignments involving R.
- The second instance or a releasable instance can satisfy a request.
- A cycle alone does not prove all processes are stuck.
- For multiple instances, use a safety or detection algorithm.
Deadlock prevention
Definition. Prevention designs the system so at least one necessary condition never holds. Methods include spooling/shareability, all-at-once requests, preemption, and total resource ordering. Prevention can reduce concurrency or cause resource underuse.
Example. A strict order on locks prevents threads from acquiring them in opposite orders.
Exam point. Prevention is a policy constraint; it does not inspect each request for future safety.
Solved examples
Example 1 — all-at-once request. P needs R1 and R2 for a task but requests R1 first, then waits for R2. Under prevention it must request both together.
- If both are available, grant both atomically.
- Otherwise grant neither and let P wait without holding one.
- Hold-and-wait is removed.
Example 2 — preemption limitation. CPU register state can be saved, but a partially written tape operation cannot safely be interrupted.
- CPU allocation is generally preemptable.
- A nonrestartable I/O operation may not be safely preemptable.
- Prevention by preemption applies only to resources whose state can be safely restored or restarted.
Deadlock avoidance and safe states
Definition. Avoidance examines each request and grants it only if the resulting state is safe. A state is safe if there exists an order in which each process can obtain its remaining maximum need from currently available resources plus resources released by earlier completed processes. Unsafe does not mean currently deadlocked.
Example. A request may be postponed even when resources are free if granting it removes every safe completion sequence.
Exam point. Need=Max−Allocation; Available is total free resources, not total system resources.
Banker's safety algorithm
Definition. For n processes and m resource types, set Work=Available, Finish[i]=false. Repeatedly find an unfinished i with Need[i]≤Work, then set Work=Work+Allocation[i] and Finish[i]=true. If all finish, the state is safe; otherwise it is unsafe.
You have read the free 30% of this note
Sign in with Google to keep reading. It is free, takes one tap, and you also get:
- 6 practice questions from this note
- Every note in full, plus the PDF library
- Official past papers as timed tests, with explanations
- An AI tutor for doubts, in Hindi or English
- Your progress, mistakes and study plan saved on every device