LIVELOCK & PRIORITY INVERSION
Everyone is busy, nothing gets done
L (priority 1) holds lock R. H (priority 3) calls lock(R) and blocks. Then M (priority 2) wakes up. The scheduler always runs the highest-priority runnable thread. Who runs next?
Two shapes of “busy but stuck”. Livelock: two threads each hold one lock, fail to get the other, politely release, sleep the same amount, and retry at the same instant, forever. Nothing completes. With spin-retry the CPU is pegged; with sleep-retry, as in the sim, the time is wasted. Priority inversion: a low-priority thread holds a lock a high-priority thread needs. The high-priority thread blocks, so it is not runnable. A medium-priority thread wakes up and, being the highest-priority runnable thread, takes the CPU from the low one. The high-priority thread now waits for the medium one, which is not even involved. The scheduler did exactly what it was told.
// Livelock: polite, and stuck. // Inversion: L holds R, H needs it, M hogs the CPU.
while (true) { low() { lock(R); work(); unlock(R); }
lock(mine); high() { lock(R); work(); unlock(R); } // waits for low()
if (!trylock(theirs)) { mid() { work(); work(); work(); } // runs instead of low()
unlock(mine); sleep(1); continue; // scheduler: highest-priority RUNNABLE thread runs.
} // H is blocked, so M beats L, so H waits for M.
work(); unlock(theirs); unlock(mine); break;
}// Livelock: break the symmetry. // Inversion: priority inheritance.
sleep(random(1, 4)); // or: always lock acquire(R, me) {
// L1 before L2 if (R.owner && R.owner.prio < me.prio)
R.owner.prio = me.prio; // boost the holder
block_until_free(R);
}
release(R) { owner.prio = owner.base; hand_off(R); } In LIVELOCK watch the round counter: both threads collide, back off by the same amount, and collide again until the progress invariant goes red. In PRIORITY INVERSION watch M run while H is blocked, then H’s deadline pass, while the scheduler invariant stays green. The scheduler is not the bug.
The scheduler is strict: a step is refused if a higher-priority thread is runnable. H has a deadline of t=7. Order the steps so H misses it because M ran while H was blocked behind L.
GOAL: H finishes after t=7, and M ran at least one tick while H was blocked
Implement priority inheritance in acquire. When the lock is held by a lower-priority thread, boost the holder to the caller's priority before blocking. The grader runs L / M / H on a strict scheduler and checks that H finishes on time, including a run where L blocks on a lock H holds.
Retry loops with fixed backoff between two services that each hold a resource the other wants. Real-time and embedded systems where an interrupt handler or a high-priority task shares a mutex with a logging or housekeeping task. Also Linux PI futexes, RTOS mutexes with a priority-inheritance flag, and database lock queues where a long-running low-priority job blocks an interactive one.
“What is the difference between deadlock and livelock?” and “What is priority inversion and how do you fix it?” Describe the three-thread shape (low holds, high waits, medium runs), explain that the scheduler is behaving correctly, and name priority inheritance or a priority ceiling as the fix. Mention Pathfinder if you want the follow-up to be about VxWorks.