bigonotes
CS ConceptsConcurrency Problems

Starvation vs Deadlock vs Livelock

How to tell the three liveness failures apart by symptoms, causes and fixes.

4 min readLast updated

Deadlock, starvation and livelock all mean "some work never finishes," but they look completely different on a running system. Telling them apart tells you where to look: blocked threads, an unfair scheduler or lock, or retry logic that keeps colliding.

Deadlock: frozen forever#

Deadlock is the easiest of the three to picture. Threads are blocked, waiting on each other in a cycle:

Thread Holds Wants State
Thread 1 A B Blocked
Thread 2 B A Blocked

Both are asleep. CPU usage is ~0%, and there's no progress.

Analogy: two cars meet on a one-lane bridge from opposite sides. Nobody moves. The four conditions and prevention strategies are covered in Deadlocks.

Starvation: forgotten forever#

Starvation is quieter. A thread is ready to run but never gets the CPU or the lock, because others keep being picked first. The system as a whole is working; only this thread is ignored.

For example, high-priority tasks keep arriving. P1, P2 and P3 (all priority 1) each run in turn, and more keep coming, while P10 (priority 5) sits in the ready queue forever.

Analogy: a polite person at a buffet. Food is being served, but every time they step forward, someone cuts in.

Common causes:

  1. Priority scheduling without aging: high-priority work never stops arriving (see CPU scheduling).

  2. Unfair mutex: a thread releases the lock and immediately re-acquires it; waiters never get a turn.

  3. Writer starvation with read-write locks: readers keep overlapping, so the read lock is never free.

    Step Readers Writer
    1 Reader 1 reading; Reader 2 starts Waiting
    2 Reader 1 finishes; Reader 3 starts Waiting
    3 Reader 2 finishes; Reader 4 starts ... Still waiting for a moment with zero readers

Fixes:

  1. Priority aging: raise a waiting thread's priority over time until it runs.
  2. Fair locks (FIFO): the longest waiter goes next. Python's threading.Lock has no fair mode, so you would build one, for example by handing turns out through a queue.Queue.
  3. Writer-preferring read-write locks: once a writer is waiting, new readers queue behind it; existing readers finish, then the writer goes.

Livelock: busy forever, going nowhere#

Livelock looks like the opposite of deadlock. Threads are not blocked. They're running and consuming CPU, but they keep reacting to each other and make no progress.

Analogy: two people in a hallway both step left, then both step right, then both left again. Both are moving, nobody gets past.

How it happens in code: both threads try to avoid deadlock by backing off, but they do it in lockstep:

Step Thread 1 Thread 2
1 lock(A): OK lock(B): OK
2 B.acquire(blocking=False): fails A.acquire(blocking=False): fails
3 unlock(A), retry unlock(B), retry
4 lock(A): OK lock(B): OK
5 B.acquire(blocking=False): fails A.acquire(blocking=False): fails
... Forever Forever

Fix: random backoff. Each thread waits a random time before retrying, which breaks the symmetry. Say each sleeps a random 1–10 ms after failing:

Time Thread 1 Thread 2
0 ms Fails, unlock(A), sleeps 7 ms Fails, unlock(B), sleeps 3 ms
3 ms Still sleeping Takes B, then A: succeeds
7 ms Takes A, then B: succeeds Done

Classic Ethernet resolves collisions the same way, with (binary) exponential random backoff. Retrying network clients use exponential backoff with jitter (a small random amount added to each wait) for the same reason.

Side-by-side#

The quickest way to tell them apart is to look at thread state and CPU usage.

Deadlock Starvation Livelock
Threads blocked? Yes No, ready but skipped No, actively running
CPU usage ~Zero for those threads Others use the CPU High, wasted
Progress None for the threads involved None for the starved thread None for the threads involved
System overall Frozen (at least partly) Working, but unfair Working, but wasteful
Typical cause Circular lock dependency Unfair scheduling or locks Symmetric retry logic
Typical fix Global lock ordering Priority aging, fair locks Random backoff
How hard to spot Easier: thread dump shows a wait cycle Hard: system looks healthy Hard: threads look busy

Key takeaways#

  • Deadlock: blocked in a cycle, zero CPU. Fix with lock ordering.
  • Starvation: ready but always skipped. Fix with aging or fair locks.
  • Livelock: running, reacting to each other, no progress. Fix with random backoff.
  • A thread dump plus CPU usage usually tells you which one you have.
Revision notes · 2 min

Core idea#

Deadlock, starvation and livelock all mean some work never finishes. Thread state and CPU usage tell them apart.

Key points#

  • Deadlock: threads block in a wait cycle. Thread 1 holds A and wants B; Thread 2 holds B and wants A.
  • Starvation: a thread is ready to run but never gets the CPU or the lock. The rest of the system works.
  • Livelock: threads run and react to each other, but nobody makes progress.
  • A thread dump plus CPU usage usually tells you which one you have.

Side by side#

Deadlock Starvation Livelock
Threads blocked? Yes No, ready but skipped No, actively running
CPU usage About zero for those threads Others use the CPU High, wasted
Typical cause Circular lock dependency Unfair scheduling or locks Symmetric retry logic
Typical fix Global lock ordering Priority aging, fair locks Random backoff
How hard to spot Easier: dump shows a cycle Hard: system looks healthy Hard: threads look busy

Starvation causes and fixes#

  • Priority scheduling without aging. Fix: raise a waiting thread's priority over time.
  • Unfair mutex: a thread releases and re-acquires at once. Fix: a fair FIFO lock.
  • Python's threading.Lock has no fair mode. Build turns with a queue.Queue.
  • Writer starvation: overlapping readers never free the read lock. Fix: a writer-preferring read-write lock.

Livelock and backoff#

  • Both threads take their first lock, fail acquire(blocking=False) on the second, release, and retry in lockstep.
  • Random backoff breaks the symmetry. Each thread sleeps a random time before it retries.
  • Classic Ethernet uses binary exponential random backoff. Network clients use exponential backoff with jitter.

Pitfalls#

  • Deadlock-avoidance retries without randomness turn a deadlock into a livelock.
  • Starvation and livelock hide well. Dashboards show a healthy, busy system.

Say it in an interview#

  • "Deadlock is blocked in a cycle at zero CPU. Fix it with lock ordering."
  • "Starvation is ready but always skipped. Fix it with aging or fair locks."
  • "Livelock is busy with no progress. Fix it with random backoff."

Found a mistake? Email ask@bigonotes.com