Operating Systems Level 7
PART 2 • RESOURCE CONTROL

Recognise, Avoid and Recover from Deadlock

Model processes and resources precisely, distinguish a deadlock from ordinary waiting, then prove whether a state is safe using the same Work and Finish reasoning used by Banker’s Algorithm.

Level 07 of 15 Intermediate 100–130 minutes Interactive safety checker
BY THE END, YOU CAN

Defend a deadlock decision

  • Identify all four Coffman conditions.
  • Read request and assignment graph edges.
  • Separate prevention, avoidance and detection.
  • Calculate Need and find a safe sequence.
  • Explain recovery costs and victim selection.
01 • DEFINE THE FAILURE PRECISELY

Deadlock Is Permanent Circular Waiting among a Set of Processes

Each process in the set waits for an event that only another process in that same set can cause—normally the release of a resource.

P1 holds R1 waits for R2
P2 holds R2 waits for R1
returns to P1

Deadlock

The involved processes cannot progress without external intervention.

Starvation

One process may wait indefinitely while other processes continue progressing.

Livelock

Processes keep changing state and reacting, but useful work does not advance.

Long waiting

A process waits for a slow event that can still complete normally.

02 • THE FOUR NECESSARY CONDITIONS

Deadlock Requires All Four Coffman Conditions at the Same Time

01

Mutual exclusion

At least one resource is non-shareable; only one process can use its instance at a time.

Example: one printer
02

Hold and wait

A process holds at least one resource while requesting additional resources held elsewhere.

Example: hold file lock, request database lock
03

No preemption

A resource cannot simply be taken away; its holder must release it voluntarily.

Example: partially updated critical state
04

Circular wait

A closed chain exists where every process waits for a resource held by the next.

Example: P1 → P2 → P3 → P1
03 • READ THE RESOURCE-ALLOCATION GRAPH

Edge Direction Tells You Who Requests and Who Holds

P1 request → R2
P2 ← assignment R2
R1 assignment → P1
R1 ← request P2

Read one edge at a time

Pi → Rj Pi requests an instance of Rj.
Rj → Pi An instance of Rj is assigned to Pi.

Single instance per resource type: a cycle is both necessary and sufficient for deadlock.

Multiple instances: a cycle is necessary but may not be sufficient because another instance can break the wait.

04 • CHOOSE WHEN TO ACT

Prevention, Avoidance and Detection Solve Different Problems

Strategy Main idea Information needed Cost or limitation
Ignore Assume deadlocks are rare; recover manually or restart. No future demand model Failure remains possible
Prevention Structurally break one Coffman condition. Rules for allocation Lower utilisation or concurrency
Avoidance Grant only requests that keep the state safe. Maximum future claims Conservative; needs advance knowledge
Detection Allow deadlock, periodically discover it, then recover. Current allocation and requests Detection and recovery overhead

Break hold and wait

Request everything together or release held resources before requesting more.

Permit preemption

Take or roll back resources when a request cannot be satisfied safely.

Order resources

Require every process to request resource types in one global order.

05 • INTERACTIVE BANKER’S ALGORITHM

Calculate Need and Search for a Safe Sequence

Edit Available, Allocation and Maximum values for three processes and three resource types. The checker validates the matrices before running the safety algorithm.

AVAILABLE
Process Allocation Maximum Need = Max − Allocation
A B C A B C A B C
SAFETY RESULT

Ready to analyse

Select a dataset or edit the values, then run the safety check.

Work and Finish trace

  1. No safety test has run yet.
06 • FIND AND BREAK AN EXISTING DEADLOCK

Detection Identifies the Set; Recovery Chooses the Damage

1

Snapshot

Collect Available, Allocation and outstanding Request.

2

Reduce

Find processes whose requests can be met by current Work.

3

Release

Simulate completion and add their allocation to Work.

4

Identify

Unfinished processes form the detected deadlocked set.

TERMINATE

Abort processes

Abort all deadlocked processes at once, or choose victims one at a time until the cycle breaks.

Consider priority, progress, resources and restart cost
PREEMPT

Take resources and roll back

Select a victim, reclaim resources, restore a safe checkpoint and retry later.

Prevent repeatedly choosing the same victim
07 • INTERACTIVE CASE ANALYSER

Classify the Situation before Choosing a Remedy

08 • CHECK YOUR UNDERSTANDING

Ten Misconception-Specific Checks

Each option explains the exact deadlock reasoning behind it.

Answered correctly: 0 of 10
09 • EXPLAIN & PREPARE

University and Placement Questions

2-MARK QUESTIONS
  1. Define deadlock.
  2. List Coffman conditions.
  3. Define a safe state.
  4. What is a request edge?
  5. What is rollback?
5-MARK QUESTIONS
  1. Explain deadlock prevention.
  2. Run Banker’s safety algorithm.
  3. Explain allocation graphs.
  4. Compare avoidance and detection.
  5. Discuss recovery methods.
INTERVIEW QUESTIONS
  1. Does a graph cycle always mean deadlock?
  2. Unsafe versus deadlocked?
  3. How does lock ordering help?
  4. What makes a good victim?
  5. Deadlock versus starvation?
Show a strong answer: “Explain Banker’s safety algorithm”
  1. Compute Need = Maximum − Allocation.
  2. Set Work = Available and every Finish value false.
  3. Find an unfinished process with Need ≤ Work component-wise.
  4. Assume it completes: Work = Work + its Allocation; set Finish true.
  5. Repeat. All true means safe and the chosen order is a safe sequence; otherwise the state is unsafe.
LEVEL 7 SUMMARY

You Can Now Separate a Cycle, an Unsafe State and a Deadlock

  • Deadlock requires mutual exclusion, hold and wait, no preemption and circular wait.
  • Graph cycles prove deadlock only when each resource type has one instance.
  • Prevention breaks a condition; avoidance preserves safety; detection acts after allocation.
  • Banker’s Algorithm proves safety by finding at least one completion sequence.
  • Recovery balances termination, rollback and victim-selection cost.
COURSE CHECKPOINT

Mark Level 7 after you can calculate Need and explain why an unsafe state is not necessarily already deadlocked.

Saved only in this browser.