Deadlocks

the four necessary conditions for deadlock

Deadlock can feel mysterious until you learn that it is impossible unless FOUR specific things are all true at the same time. Think of it like a fire needing fuel, heat, and oxygen all at once: remove any single one and the fire cannot start. Deadlock is the same — it has four 'ingredients', and if even one is missing, deadlock simply cannot happen. This is wonderful news, because it turns a vague fear into a checklist: to make deadlock impossible, just make sure one ingredient can never appear.

The four conditions (sometimes called the Coffman conditions) are: (1) MUTUAL EXCLUSION — at least one resource is held in a non-shareable way, so only one process may use it at a time. (2) HOLD AND WAIT — a process is allowed to hold resources it already has while waiting to acquire more. (3) NO PREEMPTION — a resource cannot be forcibly taken away from a process; it must be released voluntarily. (4) CIRCULAR WAIT — there exists a chain of waiting processes P1, P2, ..., Pn where each is waiting for a resource held by the next, and the last waits for one held by the first, closing the loop. All four must hold simultaneously for a deadlock to be possible.

An important honest subtlety: these four are NECESSARY but not, in general, SUFFICIENT. With multiple-instance resources you can satisfy all four and still not be deadlocked, because a spare instance may free the chain. They are guaranteed sufficient only in the single-instance case (where circular wait is exactly a cycle in the resource-allocation graph). The practical power is in the converse: break ANY one of the four — deny mutual exclusion, forbid hold-and-wait, allow preemption, or impose an ordering that kills circular wait — and you have a deadlock-prevention strategy.

Two threads each grab one lock and then wait for the other's lock: mutual exclusion (a lock is held by one thread), hold-and-wait (each holds one and waits for another), no preemption (you cannot yank a held lock away), and circular wait (T1 waits on T2, T2 waits on T1). All four are present, so a deadlock is possible.

The same two-lock hang, dissected into the four ingredients that make it possible.

The four conditions are necessary for ALL deadlocks but only sufficient when every resource has a single instance; with multiple instances, all four can hold yet no deadlock exists.

Also called
Coffman conditions科夫曼條件死結四條件