Part 4 of 7 · 2 min read
Inversion
In plain words: Suppose a high-priority process H needs a lock (Level 4, a device that only one process may use at a time) held by a low-priority process L. H must wait until L releases the lock. Now a medium-priority process M becomes ready. It has a higher priority than L, so it preempts L and runs. L cannot release the lock, and H, the most important process, waits for M, which is less important. This is priority inversion.
time: L runs and takes the lock
H becomes ready, needs the lock, waits for L
M becomes ready, preempts L <- H now waits for M as well
M finishes, L resumes, L releases the lock, H runs
The usual cure is priority inheritance: while L holds a lock that H waits for, L temporarily gets H's priority. M can then no longer preempt L, L finishes its critical section quickly, releases the lock, and returns to its own priority. A well-known case is the Mars Pathfinder lander in 1997, which reset itself because of a priority inversion, and was corrected by turning on priority inheritance for the lock. POSIX offers this with the mutex attribute PTHREAD_PRIO_INHERIT. Level 4 explains locks fully.
Priority inversion
Priority inversion is a situation in which a high-priority process is made to wait by a lower-priority process, usually because the lower one holds a resource that the higher one needs and a medium-priority process keeps the lower one from running.
Example: H waits for a lock held by L, and M preempts L, so H waits for M though H has a higher priority.
Check yourself: 1. What does H wait for in each of the three steps? 2. What does priority inheritance change? 3. Which POSIX mutex attribute turns it on?