Introduction
Deadlock prevention means designing the system so deadlock cannot occur in the first place. Instead of detecting deadlock after processes get stuck, prevention changes resource-allocation rules early.
This approach gives a strong safety guarantee, but it often reduces flexibility. The system becomes safer by restricting how processes request, hold, and release resources.
What Deadlock Prevention Means
Deadlock prevention is a strategy where the operating system or program design makes at least one necessary deadlock condition impossible. Since deadlock needs all four Coffman conditions together, breaking even one condition prevents deadlock.
The four Coffman conditions are:
Mutual exclusion: At least one resource is non-shareable.
Hold and wait: A process holds one resource while waiting for another.
No preemption: A resource cannot be forcibly taken away from its holder.
Circular wait: Processes wait for one another in a cycle.
What Deadlock Prevention Means
How Prevention Works
Deadlock prevention does not wait for a cycle to appear. It changes the resource policy before that cycle can be created.
For example, if all processes must request resources in a fixed order, circular wait is removed. If a process must request all needed resources together, hold and wait is removed.
This makes prevention different from recovery. Recovery accepts that deadlock may happen and then takes action later, while prevention tries to make the deadlock state unreachable.
Techniques for Deadlock Prevention
Each deadlock prevention technique targets one Coffman condition. The method chosen depends on the type of resource and how much restriction the system can tolerate.
Breaking mutual exclusion works only for resources that can actually be shared. A read-only file may be shared by many processes, but a printer, lock, or writable critical section may still require exclusive access.
Breaking hold and wait usually means a process must ask for all required resources at once. If everything is available, the process gets them; otherwise, it waits without holding anything.
Breaking no preemption means the system can take a resource back when safe. This is difficult for many resources because forcibly taking a lock, file, or device in the middle of use can corrupt state.
Breaking circular wait is often practical for locks. The system assigns a global order to resources, and every process must request resources according to that order.
Techniques for Deadlock Prevention
Lock Ordering Example
Lock ordering prevents circular wait by forcing every thread to acquire locks in the same sequence. If all threads follow one global order, opposite-order locking cannot create a cycle.
A simple pattern is:
if (id(A) < id(B)) { lock(A); lock(B);} else { lock(B); lock(A);}If A has a smaller ID than B, every thread locks A before B. If B has the smaller ID, every thread locks B before A.
This prevents the situation where one thread holds A and waits for B, while another thread holds B and waits for A. One thread may still wait, but it waits before holding the opposite lock, so the circular dependency does not form.
Tradeoffs of Deadlock Prevention
Deadlock prevention gives strong safety, but it is not free. The rules that prevent deadlock may also reduce concurrency, resource utilization, or design simplicity.
Important tradeoffs include:
Early resource requests can waste resources: A process may hold resources before it actually needs them.
Preemption is not always safe: Some resources cannot be taken away without damaging state.
Global ordering needs discipline: Developers must know and follow the same resource order everywhere.
Concurrency may reduce: More restrictions can make the system less flexible under load.
Prevention works best when resource rules are simple, documented, and enforceable.
Summary
Deadlock prevention designs the system so deadlock cannot occur by breaking at least one Coffman condition. It may target mutual exclusion, hold and wait, no preemption, or circular wait.
Common techniques include making resources shareable, requesting all resources together, allowing safe preemption, and enforcing fixed resource ordering. These methods improve safety, but they may reduce concurrency, flexibility, and simplicity.
Be the first to add a comment.