1. University MCA (AI/ML) Semester 1
  2. CAP366
  3. Unit 6

CAP366 · Unit 6: Deadlocks

Methods for handling deadlocks and prevention

15 practice questions · in the ETE syllabus

Key points

  • Four strategies: ignore, prevent, avoid, detect and recover
  • Prevention denies one of the four necessary conditions
  • Resource ordering prevents circular wait
  • Avoidance needs maximum claims in advance; prevention does not
  • Windows and Linux ignore deadlock among user processes
  • Kernels prevent deadlock by lock ordering, checked by lockdep or Driver Verifier

There are four ways to treat deadlocks: ignore the problem (the ostrich approach used by most general-purpose systems), prevent it, avoid it, or detect and recover.

Prevention makes sure at least one of the four necessary conditions can never hold:

Condition deniedTechniqueDrawback
Mutual exclusionMake resources sharable (e.g. read-only files, spooling)Not possible for intrinsically non-sharable resources
Hold and waitRequest all resources before starting, or release before requesting moreLow utilisation, possible starvation
No preemptionTake resources from a blocked processHard for printers and similar devices
Circular waitNumber resources and request only in increasing orderAwkward for programmers

Why ordering works: along any chain of waits the resource numbers strictly increase, so the chain cannot return to its start. If a process holding R3 needs R1, it must release R3 and request R1 then R3 again.

Avoidance differs from prevention: it needs each process's maximum demand in advance and grants a request only if the system stays in a safe state.

Exam trap: prevention constrains how requests are made by design; avoidance checks every request against future safety.

Case study: deadlocks in Windows and Linux

Neither Windows nor Linux runs the banker's algorithm or a wait-for-graph detector over ordinary user processes. Processes do not declare their maximum needs, and checking every lock request would cost too much, so both follow the ostrich approach for applications: if two threads of a program deadlock on mutexes, both simply stay blocked until the program is killed or restarted.

Inside the kernels, deadlock is prevented by lock ordering, which denies circular wait:

LinuxWindows
RuleKernel locks are taken in a fixed, documented orderDrivers and kernel code follow a lock hierarchy
Checking toollockdep records the order in which locks are taken and warns about any ordering that could form a cycleDriver Verifier (deadlock detection option) flags drivers that take locks in an order that violates the hierarchy
Hang reportingThe hung-task detector logs a warning when a task stays in uninterruptible sleep too longWait chain traversal can show which threads a hung thread is waiting on

Windows also lets a thread wait on several objects at once with WaitForMultipleObjects in wait-all mode: the objects are acquired together only when all are available, so the thread never holds some while waiting for the rest (hold and wait is avoided).

True detection and recovery is found in database servers on both systems: the lock manager looks for a cycle among waiting transactions and rolls back one victim.

Exam point: for applications the cure is the programmer's: acquire locks in the same order everywhere.

Try two questions

Question 1 · Tier 3, Apply

To deny hold and wait, a system requires each process to request all its resources before it starts running. What is the main drawback?

  1. Processes can no longer be preempted from the CPU when the timer expires
  2. A circular wait becomes more likely among the waiting processes
  3. Resources may sit idle for long and a process needing popular resources may starve
  4. Mutual exclusion is lost for the resources that are requested early
Show the answer

C. Holding everything from the start wastes resources that are only needed later, and a process needing several popular resources may wait for ever.

Why not A: Timer preemption of the CPU is unrelated to resource requests.

Why not B: Requesting everything at once removes hold and wait and does not encourage cycles.

Why not D: Resources stay exclusive; only the request pattern changes.

Question 2 · Tier 3, Apply

Resources R1 to R5 are numbered and a process may only request a resource with a higher number than any it holds. A process that currently holds R2 and R4 makes a new request. Which request is permitted?

  1. R1
  2. R5
  3. R3
  4. R2
Show the answer

B. The new request must be numbered above the highest held resource, R4, so only R5 qualifies.

Why not A: R1 is lower than the held R4 and would violate the ordering.

Why not C: R3 is lower than R4, which is already held.

Why not D: R2 is already held and lower than R4.