In our previous lessons, we've explored powerful tools for managing concurrency like mutexes and semaphores. These mechanisms are crucial for building correct and efficient multithreaded systems. However, their very power to block and coordinate processes introduces a significant risk: deadlock. This is a silent but fatal condition where parts of your system grind to a halt, with no errors or crashes—just a complete lack of progress.
This lesson is dedicated to understanding exactly why deadlocks happen. We will define the phenomenon and then dissect the four famous conditions, identified by Edward Coffman in 1971, that must all be present for a deadlock to occur. By learning to identify these conditions, you gain the fundamental diagnostic skill needed before you can prevent or resolve deadlocks in the systems you design and manage.
What is a Deadlock?
At its core, a deadlock is a state of permanent blockade. It occurs when two or more processes are stuck in a circular waiting pattern, where each process is waiting for a resource that is held by another process in the cycle.
A common analogy is a narrow one-lane bridge. If one car enters from the left and another from the right, they will meet in the middle. Neither can move forward because the other is in the way. If more cars have queued up behind them, neither can reverse. They are deadlocked. Progress is impossible without external intervention.
In a software system, this looks very similar. Consider two processes, P1 and P2, and two exclusive resources, R1 and R2 (perhaps two different database table locks).

The following article from GeeksForGeeks provides a clear, step-by-step walkthrough of how this exact scenario unfolds.
Conditions for Deadlock in Operating System
This section uses the simple two-process, two-resource model to demonstrate how a deadlock state is reached.
Please read the section titled How a Deadlock Can Occur?. Pay attention to the sequence of events: each process acquires one resource and then gets blocked trying to acquire the second. This will solidify the abstract concept with a concrete example.
As you can see, the system has entered a state where no process can make progress. This isn't a temporary delay; it's a permanent standstill.
The Four Necessary Conditions for Deadlock
The key insight from computer scientist Edward Coffman is that a deadlock situation can arise if and only if all four of the following conditions hold simultaneously in a system. If you can prove that even one of these conditions is not met, a deadlock is not possible. This makes them powerful diagnostic tools.
The following resource from AlgoMaster.io gives a fantastic overview of these four conditions.
Deadlock | Concurrency Interview | AlgoMaster.io
This article provides concise definitions, clear examples, and a summary table for the four Coffman conditions.
Please read the section from Coffman's Four Conditions down to the summary table. For each of the four conditions—Mutual Exclusion, Hold and Wait, No Preemption, and Circular Wait—focus on understanding its definition and the accompanying example. The "How to break it" points will be the focus of our next lesson.
Let's briefly review and expand upon these four conditions. To help visualize them, the following video segment offers clear, simple animations for each one.
Deadlock in Operating System | 4 Conditions of Deadlocks | Deadlock Handling - Process Management
This video from the "Simple Snippets" channel provides excellent, animated explanations for each condition.
Watch the segment from this timestamp. The presenter breaks down each of the four conditions with its own simple diagram, which helps connect the abstract definitions to visual patterns.
1. Mutual Exclusion
This condition states that at least one resource in the system must be non-shareable. Only one process can use the resource at any given time.
- In Practice: This is the entire point of a mutex, a file lock, or exclusive access to a hardware device like a printer. This condition is often inherent to the resources we need to protect and is not easily eliminated.
2. Hold and Wait
This condition occurs when a process is holding at least one resource and is simultaneously waiting to acquire additional resources that are currently held by other processes.
- In Practice: A thread locks
mutex_Ato update one data structure and then, while still holding the lock, tries to lockmutex_Bto update another. This is the "holding one thing while grabbing for another" behavior central to deadlock.
3. No Preemption
This means that a resource cannot be forcibly taken away from a process holding it. The process must release the resource voluntarily after it has completed its task.
- In Practice: Once your thread acquires a mutex, the operating system scheduler won't just take it away because a higher-priority thread needs it. The lock must be explicitly released by the holding thread. This rule provides stability but also contributes to deadlocks.
4. Circular Wait
This is the condition that closes the trap. It requires that there exists a set of waiting processes {P₀, P₁, ..., Pₙ} such that P₀ is waiting for a resource held by P₁, P₁ is waiting for a resource held by P₂, ..., and Pₙ is waiting for a resource held by P₀.
- In Practice: This is the pattern we saw in the diagram at the beginning. Process 1 waits for a resource held by Process 2, and Process 2 waits for a resource held by Process 1. This forms a cycle of dependencies from which there is no escape.
Let's apply this framework to the initial example:
- Mutual Exclusion? Yes. We assume R1 and R2 are exclusive resources like locks.
- Hold and Wait? Yes. P1 holds R1 while waiting for R2. P2 holds R2 while waiting for R1.
- No Preemption? Yes. The OS cannot just take R1 from P1 and give it to P2.
- Circular Wait? Yes. P1 waits for P2 (which holds R2), and P2 waits for P1 (which holds R1). A cycle
P1 -> R2 -> P2 -> R1 -> P1exists in the resource graph.
Since all four conditions are met, a deadlock is not just possible—it has occurred.
Conclusion
In this lesson, we moved from using concurrency tools to understanding the primary risk associated with them. Recognizing the conditions that lead to deadlock is a critical skill for any senior engineer responsible for system stability and correctness.
Key Takeaways:
- A deadlock is a permanent state where a set of processes are blocked because each process is holding a resource and waiting for another resource held by another process in the set.
- Deadlock can only occur if all four Coffman conditions are met simultaneously:
- Mutual Exclusion: At least one resource is non-shareable.
- Hold and Wait: A process holds a resource while waiting for another.
- No Preemption: Resources cannot be forcibly taken from processes.
- Circular Wait: A closed chain of processes exists, where each process waits for a resource held by the next in the chain.
Now that you can diagnose the ingredients of a deadlock, our next lesson will focus on the cure. We will explore practical strategies for deadlock prevention, which involve systematically breaking one or more of these four conditions.