1 of 4

Your RightSignal

Operating systems · 7 of 91 / 3

Deadlocks

The concept

Why two bank transfers can freeze each other forever

A deadlock happens when a group of threads each hold a lock and each wait for a lock another one in the group holds, so none of them can ever move. Nothing crashes. The threads just stop, forever.

Two transfers run at once. Alice→Bob locks Alice's account, then asks for Bob's. Bob→Alice has locked Bob's and asks for Alice's. Each waits for the other to let go, and neither will until it finishes.

Deadlock needs all four Coffman conditions: mutual exclusion (one holder per lock), hold and wait (keep locks while waiting), no preemption (nobody takes a lock away) and circular wait (waits form a loop). Remove one and it can't happen.

What it’s forTells you whether a set of threads is deadlocked, by drawing who waits for whom.

deadlock ⇔ the wait-for graph has a cycle T₁ → T₂ → … → T₁

Tᵢ
a thread (or a database transaction)
Tᵢ → Tⱼ
an arrow: Tᵢ is waiting for a lock that Tⱼ holds
cycle
a path of arrows that comes back to where it started

What it givesYes or no: the threads on a cycle are stuck forever, and nobody else will free them.

  1. T1 holds Alice, wants Bob → arrow T1 → T2
  2. T2 holds Bob, wants Alice → arrow T2 → T1
  3. T1 → T2 → T1 is a cycle, so both are deadlocked