Sub topic 17
Priority inversion
This chapter assumes you know the following:
- Priority-based preemptive scheduling.
- Mutex locks and semaphores.
- The deadlock conditions from the previous chapter.
Priority inversion is a scheduling hazard that synchronisation creates. It is a short topic, but GATE-style questions ask you to trace it.
What priority inversion is
A priority scheduler promises that the highest-priority runnable task gets the CPU. Priority inversion breaks that promise: a high-priority task is stuck waiting while a lower-priority task runs.
A short inversion is unavoidable. Suppose a low-priority task L holds a lock and a high-priority task H needs it. H must wait until L releases the lock. This is plain mutual exclusion. The wait lasts only as long as L's critical section, so it is bounded.
The dangerous case is unbounded priority inversion. It needs three tasks:
- H has high priority and needs the lock.
- M has medium priority and never touches the lock.
- L has low priority and holds the lock.
If M becomes runnable while L is inside its critical section, M preempts L. L cannot run, so it cannot release the lock. H stays blocked for as long as M runs, and M has nothing to do with H's resource. H's wait now depends on M's running time instead of L's critical-section length.
The standard scenario
Take priorities H = 3, M = 2, L = 1, where a larger number means a higher priority. The sequence is:
- L runs and acquires the mutex.
- H becomes ready and preempts L. H calls
lock()and blocks, because L holds the mutex. L resumes. - M becomes ready. M has higher priority than L, so it preempts L.
- M runs, possibly for a long time. L is ready but never scheduled, and H is blocked.
- M finishes. L resumes, releases the mutex, and H runs.
The key point is the order of the final events. H's delay includes all of M's execution time, and M can be arbitrarily long.
A real case: Mars Pathfinder
In 1997 the Mars Pathfinder lander kept resetting itself. A low-priority meteorological task held a mutex that a high-priority bus-management task needed. Medium-priority tasks preempted the low-priority one. A watchdog saw that the bus task had not completed on time and reset the system. Engineers fixed it from Earth by enabling priority inheritance on that mutex. The fix was a configuration flag in the real-time operating system (VxWorks).
Solutions
Priority inheritance
When H blocks on a lock held by L, L temporarily inherits H's priority. M can no longer preempt L, so L finishes its critical section quickly. When L unlocks, it drops back to its original priority.
Inheritance is transitive. If H waits for L, and L waits for a lock held by an even lower task, that task inherits H's priority too.
Priority inheritance does not prevent deadlock. It also does not remove the first, bounded wait. It only removes the dependence on unrelated medium-priority tasks.
Priority ceiling
Each lock gets a ceiling, which is the highest priority of any task that ever uses it. In the immediate ceiling variant (also called the highest locker protocol), a task that acquires the lock immediately runs at the ceiling priority. Two properties follow:
- No task that could contend for the lock can preempt the holder, so the holder always finishes its critical section.
- Under the original priority ceiling protocol, with its additional rule for granting locks, a task is blocked by at most one lower-priority critical section, and deadlock among those locks cannot occur.
The cost is that you must know every user of each lock in advance. A task may also be boosted when no one is competing for the lock.
Other approaches
- Disable interrupts or preemption in short critical sections. This is common inside kernels but unsafe for long sections.
- Avoid sharing between tasks of different priority. Use message passing, or lock-free structures, instead of a shared mutex.
- Random boosting. Windows occasionally boosts the priority of threads that have been starved for a while. It does not guarantee a bound, but it eventually breaks the inversion.
| Technique | When the boost happens | Needs advance knowledge of lock users | Prevents deadlock |
|---|---|---|---|
| Priority inheritance | When a higher-priority task blocks | No | No |
| Immediate priority ceiling | When the lock is acquired | Yes (to set the ceiling) | Yes, for those locks |
| Disabling preemption | Whole critical section | No | Not applicable |
Spinlocks and inversion
The topic spine links priority inversion to spinlocks and mutexes, and the connection is worth stating. A spinlock makes inversion worse on a single CPU. Suppose H spins on a lock held by L. H keeps the CPU busy, so L is never scheduled and can never release the lock. This is a livelock-like deadlock. It is not a classical deadlock, because there is no cycle of waiting. The real problem is that the waiter is burning the CPU the holder needs.
For this reason, a user-space spinlock on one CPU with strict priorities is a bug. A blocking mutex lets H sleep, so L can run. Kernels avoid the problem by disabling preemption while a spinlock is held, which means a spinlock holder cannot be preempted by anything, including a higher-priority task on that CPU.
Requesting inheritance in POSIX
POSIX threads let you choose the mutex protocol. The program below creates a mutex with priority inheritance and locks and unlocks it once. Compile it with gcc -pthread.
#include <pthread.h>
#include <stdio.h>
#include <string.h>
int main(void) {
pthread_mutexattr_t attr;
pthread_mutex_t m;
int rc;
pthread_mutexattr_init(&attr);
rc = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
if (rc != 0) {
fprintf(stderr, "setprotocol failed: %s\n", strerror(rc));
return 1;
}
pthread_mutex_init(&m, &attr);
pthread_mutexattr_destroy(&attr);
pthread_mutex_lock(&m);
puts("holding a priority-inheritance mutex");
pthread_mutex_unlock(&m);
pthread_mutex_destroy(&m);
return 0;
}You can also pass PTHREAD_PRIO_PROTECT to get a ceiling protocol. In that case you set the ceiling with pthread_mutexattr_setprioceiling. Inheritance only has an effect when threads use a real-time scheduling policy such as SCHED_FIFO, which usually needs elevated privileges.
Common errors
- Calling any wait for a lock "priority inversion". Inversion needs a third task that runs while the holder is stalled. A simple wait for a lock is just mutual exclusion.
- Thinking inheritance raises the priority permanently. The boost ends when the lock is released.
- Thinking semaphores are covered by inheritance. A binary semaphore has no owner. The system cannot tell which task to boost, so many operating systems apply inheritance only to mutexes.
- Expecting inheritance to fix deadlock. It fixes the scheduling problem only.
Worked trace
Priorities are H = 3, M = 2, L = 1. Their arrival times and CPU demands are:
- L arrives at t = 0 and takes the lock at once. It needs 3 units of CPU while holding it.
- H arrives at t = 1 and tries to take the lock. It needs 1 unit of CPU while holding it.
- M arrives at t = 2 and needs 5 units of CPU. It never uses the lock.
Without inheritance:
- L runs from 0 to 1.
- H arrives at 1 and blocks. L runs from 1 to 2.
- M arrives at 2 and preempts L. M runs from 2 to 7.
- L runs from 7 to 8 and releases the lock.
- H runs from 8 to 9.
H finishes at t = 9. With inheritance:
- L inherits priority 3 at t = 1 and runs until t = 3, when it releases the lock.
- H runs from 3 to 4.
- M runs from 4 to 9.
H finishes at t = 4, and M finishes at t = 9 in both cases.
Exercise
Three tasks share one mutex. L (priority 1) holds it. H (priority 3) is blocked waiting for it. M (priority 2) becomes ready and has no use for the mutex. Without any protocol, who runs, and which protocol change lets L finish first?
Show answer
M runs, because it has higher priority than L and H is blocked. L cannot release the mutex, so H waits for M, which is unbounded priority inversion. With priority inheritance, L runs at priority 3 while H waits, so M cannot preempt it. L finishes its critical section, releases the mutex, and H runs. A priority ceiling set to 3 gives the same effect from the moment L locks the mutex.
Exercise
A single-CPU system runs a high-priority task H that spins on a spinlock held by a preempted low-priority task L. Is this a classical deadlock? Why does it still make no progress?
Show answer
It is not a classical deadlock, because there is no circular wait among resources. It makes no progress because H busy-waits and keeps the CPU while L, which needs the CPU to release the lock, never runs. Replacing the spinlock with a blocking mutex, or disabling preemption while the lock is held, fixes it.