Sub topic 17
Priority Inversion
This chapter assumes that you understand processes, threads, preemption, mutex locks, and semaphores. A mutex is a lock that allows only one thread to enter a protected section at a time. Preemption is when the scheduler pauses a running thread so another thread can run.
What priority inversion means
Priority inversion occurs when a high-priority thread waits for a lock held by a low-priority thread, while a medium-priority thread prevents the low-priority thread from running and releasing the lock.
The high-priority thread is therefore delayed by a lower-priority thread. The medium-priority thread may make the delay worse even though it does not use the lock.
Consider three threads:
- L, with low priority, acquires mutex
M. - H, with high priority, needs
Mand blocks. - M, with medium priority, becomes ready and runs.
While H waits, the scheduler chooses M instead of L. Thread L cannot run long enough to leave its critical section. Thread H remains blocked behind L.
This is called inversion because the effective order is opposite to the intended priority order.
Why it matters
Priority inversion can cause a high-priority task to miss a deadline. In real-time systems, that can be more serious than ordinary scheduler unfairness.
Priority inversion is different from deadlock:
- In priority inversion, the low-priority owner can eventually release the lock.
- In deadlock, threads wait forever because each required resource is held by another waiting thread.
The delay can still become unbounded if medium-priority work keeps arriving.
Preventing unbounded inversion
Operating systems commonly use priority protocols.
| Protocol | Main idea | Cost or limitation |
|---|---|---|
| Priority inheritance | Temporarily raise the lock owner's priority to the highest priority of its blocked waiters | The scheduler must track ownership and waiting |
| Priority ceiling | Give each lock a predefined ceiling priority and raise the owner to that level | Requires careful lock-priority design |
With priority inheritance, when H blocks on a mutex owned by L, L temporarily executes at H's priority. The medium-priority thread M cannot preempt L. Thread L finishes its critical section, releases the mutex, and returns to its original priority.
Priority inheritance bounds the inversion caused by unrelated medium-priority threads. It does not fix a critical section that is inherently too long, and it does not remove deadlock caused by inconsistent lock ordering.
Exam method
For a scheduling question:
- List each thread's priority.
- Record which thread owns each lock.
- Mark blocked high-priority threads.
- Check whether a medium-priority ready thread can preempt the lock owner.
- Apply priority inheritance or ceiling rules if the question specifies them.
- Continue until the lock is released.
Exercise
L owns mutex M. H, which has higher priority, blocks on M. M has medium priority and is ready. Under priority inheritance, which thread runs next and why?
Show answer
L runs at H's temporarily inherited priority. It can finish its critical section and release M before M preempts it.