What is a livelock and what is starvation?
Short answer: In a livelock, tasks keep running and changing state but make no progress, for example two tasks that detect a conflict, both back off and retry at the same moment forever. Starvation means a task never gets the CPU or a resource because others always win; in a fixed-priority RTOS, a high-priority task that never blocks starves every lower-priority task, including the idle task.
Livelock examples and fixes
- Two tasks using "try lock A then B; if B fails, release A and retry" in lock-step: add random back-off or a fixed lock order.
- Network devices retransmitting after collisions at the same time: Ethernet's randomised exponential back-off exists to avoid exactly this.
- Two tasks politely yielding to each other forever.
Starvation examples and fixes
| Cause | Fix |
|---|---|
| High-priority task busy-waits/polls | make it block on events; never poll in a high-priority task |
| Too much work at high priority | move work to lower priorities, reduce rates, or a faster CPU |
| Unfair locks (spinlocks, readers starving writers) | fair locks (ticket, FIFO), writer preference |
| Aging not used in priority schedulers | increase the priority of long-waiting tasks over time (general-purpose OSes) |
Symptoms of starvation in an RTOS: the idle task never runs (CPU load 100%), the watchdog starts resetting the system, low-priority logging or communication stops, and vApplicationIdleHook (where you might sleep or kick a watchdog) never executes.
Monitoring: measure CPU load and per-task run time (configGENERATE_RUN_TIME_STATS), and track the stack and execution time of high-priority tasks.