Facebook Pixel

Livelock in Operating Systems

We spend a lot of time talking about deadlocks, where processes completely freeze and stop executing. But there is a closely related, equally frustrating problem where processes keep executing, yet still fail to get any work done.

This is called a livelock.

What is Livelock?

A livelock is a situation where two or more processes continuously change their states in response to changes in the other processes. Unlike a deadlock, the processes are not blocked or sleeping. They are awake, actively running, and constantly doing things.

The problem is that their actions perfectly counter each other, trapping them in an endless loop of state changes that never results in any actual progress.

How Livelock Occurs

Livelocks almost always occur as an unintended side effect of algorithms designed to PREVENT deadlocks. When a system detects a potential conflict, it tells the involved processes to back off, drop their resources, and try again.

If the retry mechanism is completely deterministic (meaning it behaves exactly the same way every time), the processes might back off simultaneously, wait the exact same amount of time, and try again simultaneously—resulting in another collision. They get trapped in an endless cycle of colliding, backing off, and retrying.

Example of Livelock

The most intuitive example of a livelock happens in real life all the time.

The Hallway Dance
Imagine two people walking toward each other in a narrow hallway. To avoid a collision (a deadlock), Person A steps to their right. At the exact same moment, Person B steps to their left. They are still blocking each other. So, Person A steps to their left, and Person B steps to their right. Still blocked. They keep mirroring each other's movements back and forth. Neither is standing still, but neither can pass.

In an operating system context, a classic example involves network communication:

text
1. Two network nodes try to send a packet on a shared line at the same time.
2. A collision occurs.
3. Both nodes detect the collision and execute a recovery algorithm: "Wait 10 milliseconds, then resend."
4. Both nodes wait exactly 10ms.
5. Both nodes resend exactly at the same time.
6. Another collision occurs. The cycle repeats forever.

How do we fix this? By introducing randomness. If the recovery algorithm says "Wait a RANDOM amount of time between 1ms and 20ms," one node will almost certainly retry before the other, breaking the synchronized loop.

Effects of Livelock

Livelocks can actually be worse for system health than deadlocks because of how they interact with the CPU.

  • CPU Waste: A deadlocked process goes to sleep. It consumes zero CPU cycles. A livelocked process is actively running, burning through CPU cycles just to change its state back and forth. This starves other healthy processes of CPU time.
  • No Progress: Just like a deadlock, the tasks assigned to the livelocked processes are never completed.
  • Hard to Detect: Because the processes are still changing state and using the CPU, traditional deadlock detection algorithms (like cycle detection in graphs) will completely miss them. To the OS, they look like processes doing normal, heavy work.

Deadlock vs Livelock

FeatureDeadlockLivelock
Process StateBlocked / SleepingActive / Running
State ChangesNone. Everything is frozen.Constant. Processes continuously change state.
CPU UtilizationZero (processes are asleep).High (processes are spinning in a loop).
Root CauseHolding resources while waiting for others.Poorly designed collision avoidance/retry logic.
Real-World AnalogyTwo cars stuck at a 4-way stop, neither willing to move.Two people stepping side-to-side in a hallway.

Summary

Livelock is the active cousin of deadlock. Instead of freezing up, processes trapped in a livelock burn energy running in circles.

It is a harsh reminder that simply forcing processes to "back off and try again" when they encounter a conflict is not enough. Without randomness or a central coordinator, deterministic retry loops will just recreate the exact same conflict over and over.

Sort the Concepts

Classify the following scenarios as either a Deadlock or a Livelock.

Deadlock
Livelock
Unsorted Items:
Four cars arrive at an intersection simultaneously, and all four refuse to move until the car on their right goes first.
Two people call each other at the exact same time, get busy signals, hang up, and instantly dial each other again repeatedly.
Two threads continuously yield the CPU to each other out of politeness, but neither actually executes their critical task.
Transaction A locks Table 1 and waits for Table 2. Transaction B locks Table 2 and waits for Table 1. Both go to sleep.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.