What is the difference between deadlock, starvation and livelock?
MediumConceptDeadlocks
Answer
In all three, some process fails to make progress, but for different reasons.
- Deadlock: processes are blocked forever in a circular wait. Nothing changes, and outside action, such as killing a process, is needed to break it.
- Starvation: a process waits indefinitely because others are always chosen first, such as a low-priority job under strict priority scheduling, while the rest of the system keeps working. It is fixed with aging or fair queuing.
- Livelock: processes are not blocked; they keep changing state in response to each other, yet no useful work gets done. It is usually fixed with random back-off, which means waiting a random time before retrying.
- Every deadlocked process is also starving, but starvation can occur without any deadlock.
Example: two people meet in a narrow corridor. Deadlock: both stand still, each waiting for the other to step aside. Livelock: both step left, then both step right, again and again. Starvation: a polite person keeps letting everyone else pass first and never gets through. In code, livelock appears when two threads each call tryLock() on the other's lock, fail, release their own lock and retry at exactly the same moment, over and over.