What is a Deadlock?
Learn what a deadlock is — the four Coffman conditions, prevention, avoidance and detection — with examples and operating systems interview questions.
Expected Interview Answer
A deadlock is a situation where a set of processes are permanently blocked because each holds a resource the others need and is waiting for a resource another process holds, so none can ever proceed.
A deadlock can only occur when all four Coffman conditions hold simultaneously: mutual exclusion (a resource is held in a non-shareable mode), hold and wait (a process holds resources while waiting for more), no preemption (resources cannot be forcibly taken away), and circular wait (a closed chain of processes each waiting on the next). Operating systems handle deadlocks by prevention (breaking one condition), avoidance (e.g. the Banker’s algorithm), detection and recovery, or the ostrich approach of ignoring them when they are rare. Breaking any single Coffman condition is enough to make deadlock impossible.
- Understanding the four Coffman conditions to reason about liveness
- Knowing prevention, avoidance, detection and recovery strategies
- Recognizing circular wait via resource-allocation graphs
- Designing lock ordering that avoids circular dependencies
AI Mentor Explanation
A deadlock is like two batters stranded mid-pitch, each refusing to run until the other moves first — both wait forever and no run is scored. Each holds their crease (a resource) and needs the other’s ground to complete the run. Neither will give up their end (no preemption) and their waiting forms a closed loop. The umpire could break it by forcing one back, just as an OS breaks a Coffman condition.
Step-by-Step Explanation
Step 1
Mutual exclusion
At least one resource is held in a non-shareable mode — only one process at a time.
Step 2
Hold and wait
A process holds resources while waiting to acquire additional ones.
Step 3
No preemption
Resources cannot be forcibly taken; they are released only voluntarily.
Step 4
Circular wait
A closed chain of processes each waits for a resource held by the next.
What Interviewer Expects
- A precise definition of permanent mutual blocking
- All four Coffman conditions and that all must hold
- Handling strategies: prevention, avoidance, detection, recovery
- That breaking any one condition prevents deadlock
Common Mistakes
- Confusing deadlock with livelock or plain starvation
- Listing only some of the four Coffman conditions
- Thinking deadlock needs a majority of conditions rather than all four
- Not knowing any concrete avoidance method (e.g. Banker’s algorithm)
Best Answer (HR Friendly)
“A deadlock is when several tasks get permanently stuck because each is waiting for a resource another is holding, and none will let go. It happens only when four specific conditions all line up, so systems avoid it by breaking one of them — often by making everyone request shared resources in the same order.”
Code Example
import threading
lock_a = threading.Lock()
lock_b = threading.Lock()
def t1():
with lock_a:
with lock_b: # needs B while holding A
pass
def t2():
with lock_b:
with lock_a: # needs A while holding B → circular wait
pass
# Fix: always acquire lock_a before lock_b in every thread
# (a consistent global lock order breaks the circular-wait condition)Follow-up Questions
- What is the difference between deadlock, livelock and starvation?
- How does the Banker’s algorithm avoid deadlock?
- What is a resource-allocation graph and how does a cycle indicate deadlock?
- How does consistent lock ordering prevent deadlock?
MCQ Practice
1. How many Coffman conditions must hold for a deadlock to occur?
Deadlock requires all four conditions — mutual exclusion, hold and wait, no preemption, and circular wait — simultaneously.
2. Which strategy tries to break the circular-wait condition?
Requiring every process to acquire resources in the same fixed order makes a circular wait impossible.
3. The Banker’s algorithm is an example of deadlock?
The Banker’s algorithm avoids deadlock by only granting requests that leave the system in a safe state.
Flash Cards
What is a deadlock? — Processes permanently blocked, each holding a resource the others need and waiting on another’s.
The four Coffman conditions? — Mutual exclusion, hold and wait, no preemption, circular wait — all must hold.
How to prevent deadlock? — Break any one condition — e.g. enforce a global lock-acquisition order to kill circular wait.
Banker’s algorithm? — A deadlock-avoidance method that grants requests only if the system stays in a safe state.