---
title: "Deadlock occurrence and avoidance methods"
subject: "Operating System"
chapter: 7
key: "semester_6/operating_system:deadlock-avoidance-methods-examples"
source: https://www.bctengineer.com/q/semester_6/operating_system/deadlock-avoidance-methods-examples
site: BCT Engineering
---

# Deadlock occurrence and avoidance methods

Operating System · Chapter 7: Deadlock

- Asked in: 2070 Magh, 2+2+6 marks
- Video: [Operating System Deadlock Prevention & Avoidance | GATE, University Exams, Placement Ready](https://www.youtube.com/watch?v=e5mEzdVXMDY) — Code Hacker
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/deadlock-avoidance-methods-examples
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-7

## Question

What is deadlock? How does it occur? Explain various deadlock avoidance methods with examples.

## Short answer

Deadlock occurs when all four conditions hold. Avoidance ensures safety: the resource-allocation graph algorithm (single instance) checks for cycles; the Banker's algorithm (multiple instances) uses Available, Max, Allocation, Need, granting requests only if a safe sequence exists, e.g., P1→P0→P2 with 12 resource units.

## Answer

#### Deadlock
A set of processes is in deadlock when each is waiting for a resource held by another process in the set, so none can proceed.

#### How deadlock occurs
It occurs when four conditions hold simultaneously: mutual exclusion, hold and wait, no preemption and circular wait. Example: P1 holds tape drive and requests printer; P2 holds printer and requests tape drive; both wait forever.

#### Deadlock avoidance
Avoidance means the OS examines each resource request and grants it only if the system stays in a **safe state**, using advance information on the maximum needs of each process. A state is safe if there is a sequence of all processes in which each can get its maximum needs and finish.

**1. Resource-allocation graph algorithm** (single instance per resource)
Add *claim edges* Pi → Rj showing that Pi may request Rj later. A request is granted (claim edge converted to assignment edge) only if it does not form a cycle in the graph.

**2. Banker's algorithm** (multiple instances)
Data: Available, Max, Allocation, Need = Max − Allocation.
- Safety algorithm: Work = Available; find process with Need ≤ Work; Work += Allocation; repeat; if all can finish the state is safe.
- Resource-request algorithm: for a request, check Request ≤ Need and ≤ Available; pretend to allocate; run the safety algorithm; grant only if safe, otherwise the process waits.

**Example:** 3 processes, one resource type with 12 units. Max needs: P0 = 10, P1 = 4, P2 = 9. Currently allocated: P0 = 5, P1 = 2, P2 = 2, so Available = 3.

| Process | Alloc | Max | Need |
|---|---|---|---|
| P0 | 5 | 10 | 5 |
| P1 | 2 | 4 | 2 |
| P2 | 2 | 9 | 7 |

P1 (need 2 ≤ 3) finishes → Work = 5; P0 (need 5 ≤ 5) finishes → Work = 10; P2 (need 7 ≤ 10) finishes. Safe sequence: P1, P0, P2. If P2 now asked for 1 more unit, and the system pretended to grant it: Available = 2, P1 could finish → 4, but P0 needs 5 and P2 needs 6 > 4, so unsafe; the request is delayed.

Avoidance is less strict than prevention but needs prior knowledge of maximum demands and a fixed number of processes.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does avoidance do on each request?
   A. Grants every request
   B. Grants it only if the system stays in a safe state
   C. Denies requests when resources are held
   D. Aborts a process at random
   Answer: B. It uses advance information on maximum needs. (Key point: Safe state)

2. When is a claim edge converted to an assignment edge?
   A. Only if no cycle forms in the graph
   B. Always
   C. Only if the resource is sharable
   D. Only if the process has priority
   Answer: A. This works for single-instance resources. (Key point: RAG algorithm)

3. 12 units, P0 max 10 alloc 5, P1 max 4 alloc 2, P2 max 9 alloc 2. Safe sequence?
   A. P0, P1, P2
   B. P2, P0, P1
   C. P1, P0, P2
   D. P0, P2, P1
   Answer: C. Available 3 lets P1 finish (Work 5), then P0 (10), then P2. (Key point: Example safe sequence)

4. In the resource-request algorithm, what if pretend-allocation is unsafe?
   A. Request is granted anyway
   B. All processes abort
   C. Available is doubled
   D. The process waits and the request is delayed
   Answer: D. In the example P2 asking 1 more is unsafe. (Key point: Request algorithm)

## Related in Operating System

- [Avoidance, detection, prevention techniques](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-avoidance-detection-prevention) — Operating System, chapter 7

## Related in other subjects

- [Lock, deadlock; deadlock avoidance](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Alock-deadlock-avoidance) — Distributed System, chapter 8
