Deadlock Prevention

Deadlock prevention is a design-time strategy that structures a system so at least one of the four necessary deadlock conditions (mutual exclusion, hold and wait, no preemption, circular wait) can never occur. Since mutual exclusion usually cannot be removed, prevention targets the other three, for example by requiring processes to request all their resources up front.

3 min read

Deadlock prevention is a design-time strategy: instead of detecting and fixing a deadlock after it happens, you structure the system so at least one of the four necessary conditions (mutual exclusion, hold and wait, no preemption, circular wait) can never occur at all.

Attacking each condition

Mutual exclusion. This one usually can't be removed, some resources (a printer, a write lock on a row) are inherently exclusive by nature. Prevention strategies generally leave this condition alone and target the other three instead.

Hold and wait. Require a process to request all the resources it will ever need up front, before it starts running, and grant them all together or none at all. If a process can't hold one resource while waiting for another, this condition is broken. The downside is poor resource utilization: a process might sit idle holding resources it won't need until much later.

No preemption. Allow the OS to forcibly take a resource away from a process if that process is blocked waiting for another resource, and give it back later. This works well for resources whose state can be saved and restored (like CPU registers) but poorly for resources that can't be safely interrupted mid-use (like a print job halfway through).

Circular wait. Impose a strict, global ordering on all resource types, and require every process to request resources only in increasing order. If R1 comes before R2 in the ordering, no process can hold R2 and then request R1, which makes a circular wait mathematically impossible: a cycle would require some process to request a lower-numbered resource while holding a higher-numbered one, violating the ordering rule.

A worked example of resource ordering

Suppose the system defines an order: Printer (1), Scanner (2), Disk (3). Every process must request resources in increasing numeric order only.

  • P1 needs the Printer and then the Scanner: requests Printer (1), then Scanner (2). Valid, increasing order.
  • P2 needs the Scanner and then the Printer: it would need to request Scanner (2) then Printer (1), decreasing order, which is disallowed. P2 must instead request Printer (1) first (even though it doesn't strictly need it yet), then Scanner (2).

Because every process is forced through the same increasing sequence, two processes can never end up each holding what the other needs in reverse order, the exact shape a circular wait requires.

Where this matters in real systems

Resource ordering is a genuinely common real-world technique. Database systems that need to lock multiple rows in a single transaction often sort the rows by primary key before acquiring locks, specifically to guarantee every transaction acquires locks in the same order and a circular wait between two transactions becomes impossible.

What interviewers actually ask about deadlock prevention

  • "How do you prevent hold and wait?" Require processes to request all needed resources at once, upfront, rather than incrementally.
  • "How do you prevent circular wait?" Impose a global ordering on resource types and require all requests to follow increasing order.
  • "What's the tradeoff of deadlock prevention compared to just letting deadlocks happen and detecting them?" Prevention avoids deadlocks entirely but often at the cost of resource utilization or throughput (processes over-request resources, or wait longer than strictly necessary); detection allows more efficient resource use but requires ongoing overhead to check for and recover from deadlocks when they do occur.
  • "Why is mutual exclusion usually left alone in prevention strategies?" Because many resources are inherently exclusive by their real nature (you can't have two processes safely writing to the same file simultaneously), so removing this condition often isn't practically possible.

Key takeaways

  • Prevention works by structurally ensuring at least one of the four necessary conditions can never occur.
  • Requiring all resources upfront breaks hold and wait; allowing preemption breaks no preemption; a global resource ordering breaks circular wait.
  • Mutual exclusion is usually left in place since many resources are genuinely exclusive.
  • Resource ordering by a consistent key (like sorting rows by primary key before locking) is a real technique used in production database systems.
All 205 OS interview questions

Browse all Operating Systems topics

Log in to save which topics you've read across every subject.

Frequently asked questions

How do you prevent hold and wait?

Require processes to request all needed resources at once, upfront, rather than incrementally.

How do you prevent circular wait?

Impose a global ordering on resource types and require all requests to follow increasing order.

What's the tradeoff of deadlock prevention compared to just letting deadlocks happen and detecting them?

Prevention avoids deadlocks entirely but often at the cost of resource utilization or throughput (processes over-request resources, or wait longer than strictly necessary); detection allows more efficient resource use but requires ongoing overhead to check for and recover from deadlocks when they do occur.

Why is mutual exclusion usually left alone in prevention strategies?

Because many resources are inherently exclusive by their real nature (you can't have two processes safely writing to the same file simultaneously), so removing this condition often isn't practically possible.