---
title: "Avoidance, detection, prevention techniques"
subject: "Operating System"
chapter: 7
key: "semester_6/operating_system:deadlock-avoidance-detection-prevention"
source: https://www.bctengineer.com/q/semester_6/operating_system/deadlock-avoidance-detection-prevention
site: BCT Engineering
---

# Avoidance, detection, prevention techniques

Operating System · Chapter 7: Deadlock

- Asked in: 2072 Magh, 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-detection-prevention
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-7

## Question

What is deadlock avoidance and detection? Explain all possible deadlock prevention techniques.

## Short answer

Avoidance grants requests only if the system stays in a safe state (Banker's algorithm). Detection lets deadlock occur, then finds it via graph or matrix and recovers. Prevention breaks one condition: share resources, request all at once, preempt if blocked, or impose a resource ordering to prevent circular wait.

## Answer

#### Deadlock avoidance
The OS has advance information on the maximum resources each process may request. It grants a request only if the resulting state is **safe**, i.e. there is some order in which all processes can complete. The Banker's algorithm (Dijkstra) is the standard method. Unsafe states are not necessarily deadlocked, but are avoided.

#### Deadlock detection
The system does not prevent or avoid deadlock; it lets it occur, then detects it by examining a resource allocation graph (cycle means deadlock) or by the matrix detection algorithm, and recovers by terminating processes or preempting resources.

#### Deadlock prevention techniques
Prevention ensures that at least one of the four necessary conditions can never hold.

1. **Attacking mutual exclusion:** make resources sharable where possible (e.g. spooling a printer so only the spooler daemon uses it). Not all resources can be shared.
2. **Attacking hold and wait:** require a process to request all its resources before starting (or to release all it holds before asking for more). Drawbacks: low resource utilisation, starvation, and the need to know all requirements in advance.
3. **Attacking no preemption:** if a process holding resources requests another one that is unavailable, all its held resources are preempted (released implicitly) and it restarts later when all can be had. Works for state-saveable resources (CPU registers, memory), not for printers.
4. **Attacking circular wait:** number all resource types and require processes to request resources only in increasing numerical order (or hold only one at a time). A cycle can then never form. Drawback: finding a natural ordering that suits all processes is hard.

| Approach | Idea | Cost |
|---|---|---|
| Prevention | Break one condition | Low utilisation |
| Avoidance | Stay in safe state | Needs max claims |
| Detection | Find and recover | Recovery overhead |

## Check yourself

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

1. In deadlock avoidance, when does the OS grant a request?
   A. Only if the resulting state is safe, i.e. some order exists in which all processes can finish
   B. Only if no other process is using the resource
   C. Always, and kill a process if deadlock occurs
   D. Only if the process holds no other resources
   Answer: A. Avoidance keeps the system in a safe state using advance knowledge of maximum claims (Banker's algorithm). (Key point: Safe state)

2. What advance information does avoidance require?
   A. The CPU burst time of each process
   B. The maximum resources each process may request
   C. The numbering of all resource types
   D. The checkpoints of each process
   Answer: B. Without maximum claims the OS cannot test whether a state is safe. (Key point: Avoidance requirement)

3. How does deadlock detection differ from prevention and avoidance?
   A. It stops one of the four conditions from ever holding
   B. It only grants requests that keep the state safe
   C. It lets deadlock occur, finds it (cycle in RAG or matrix algorithm), then recovers
   D. It ensures resources are requested in increasing order
   Answer: C. Detection allows deadlock first and then recovers by terminating or preempting. (Key point: Detection approach)

4. How can hold and wait be prevented, and what is the drawback?
   A. Request resources in numerical order; hard to find an ordering
   B. Spool the device; not all resources can be spooled
   C. Preempt resources; only works for printers
   D. Request all resources before starting; low utilisation and need to know needs in advance
   Answer: D. Requiring all resources up front breaks hold and wait but wastes resources and needs advance knowledge. (Key point: Hold and wait prevention)

## Related in Operating System

- [Deadlock occurrence and avoidance methods](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-avoidance-methods-examples) — Operating System, chapter 7
- [Deadlock, postponement, prevention, 2 processes](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-postponement-two-process-three-resource) — Operating System, chapter 7
- [Deadlock conditions, detection, action](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-conditions-detection-recovery-action) — Operating System, chapter 7
- [Four deadlock conditions; prevention, recovery](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-conditions-prevention-recovery) — Operating System, chapter 7
- [Necessary conditions; deadlock prevention](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-prevention-necessary-conditions) — Operating System, chapter 7
- [Deadlock detection with allocation matrices](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adeadlock-detection-matrices) — 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
- [Error detection in serial data transmission](https://www.bctengineer.com/question.md?key=semester_5%2Finstrumentation_ii%3Aserial-error-detection) — Instrumentation II, chapter 3, asked 2×
- [Dataword vs codeword; error coding techniques](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Adataword-codeword-error-coding) — Data Communication, chapter 8
- [Cavitation effects and prevention in reaction turbines](https://www.bctengineer.com/question.md?key=semester_7%2Fhydropower_engineering%3Acavitation-effects-prevention) — Hydropower Engineering, chapter 7
