The Ultimate Guide to Deadlocks: Why Your Computer Gets Stuck
Have you ever been stuck in a traffic jam on a narrow bridge? Imagine two cars meet in the middle, face-to-face. Neither can move forward because the other car is blocking the way. Neither can move back because there are cars behind them. Everyone is stuck waiting… forever.
In the world of computers, we call this a Deadlock.
Get Vihanga Sathsara’s stories in your inbox
Join Medium for free to get updates from this writer.
It’s a situation where a group of processes (running programs) are blocked indefinitely. Each process is holding onto a resource (like a printer or a file) and waiting for another resource that someone else is holding. It’s the ultimate staring contest, and nobody blinks!
Deadlock Characterization
For a deadlock to happen, it’s not just bad luck. Four specific conditions must happen at the same time. If we can break just one of these, the deadlock disappears!
- Mutual Exclusion : This means a resource can only be used by one person at a time. Think of a printer — only one document can print at once. If everyone could share everything instantly, we wouldn’t have to wait!
- Hold and Wait : This is when a process is holding onto some resources (like having the flour and sugar) but is stuck waiting for more resources (the eggs and milk) currently held by others.
- No Preemption : This means we can’t be rude. We cannot forcibly take a resource away from a process; it has to give it up voluntarily after it’s done.
- Circular Wait : This is the classic loop. Process A is waiting for Process B, Process B is waiting for Process C, and Process C is waiting for Process A. It’s a closed circle of waiting.
How Do We Fix It? 3 Main Strategies
Computer scientists have come up with three main ways to deal with these digital traffic jams.
1. Prevention:
This strategy tries to make sure a deadlock is mathematically impossible by breaking one of those 4 conditions we just listed.
- Breaking “Hold and Wait”: We could make a rule that a process must ask for everything it needs before it starts. If you need a printer, scanner, and hard drive, you grab them all at once. If you can’t get all three, you get none. The downside? It’s not very efficient (Low Resource Utilization) because you might hog the printer for hours even if you only use it for the last minute.
- Breaking “Circular Wait”: We can force an order on resources. For example, “You must always ask for the Scanner (Resource 1) before the Printer (Resource 2).” If everyone follows the order, a circle can never form!
2. Avoidance:
This strategy allows the system to make dynamic decisions. The most famous method is the Banker’s Algorithm.
- Every process tells the system in advance the maximum resources it might need.
- When a process asks for a resource, the system pauses and calculates: “If I give you this, will we still be safe?”
- If the answer is YES (we are in a Safe State), the request is granted.
- If the answer is NO (it might lead to a deadlock later), the process has to wait.
3. Detection & Recovery:
Some systems just let deadlocks happen because preventing them is too much work. instead, they run a check every once in a while to see if there is a cycle in the Resource-Allocation Graph. If they find a deadlock, they have to recover. This usually means playing the bad guy:
- Abort Process: They might kill the deadlocked processes to break the cycle.
- Preempt Resources: They might forcibly take a resource away from a process and give it to someone else.
The Hidden Danger: Starvation
There is one last trap. If we always choose to kill the “low priority” process to solve a deadlock, that poor process might never get to run. It gets killed, restarts, and gets killed again. This is called Starvation.
To fix this, we use Aging. This means we gradually increase a process’s priority the longer it waits. Eventually, even the smallest process becomes the most important VIP in the system!









