Priority inversion: what it is and how inheritance fixes it
Priority inversion is a classic real-time bug, and a classic interview question. It famously caused repeated resets on the Mars Pathfinder lander in 1997, and it can happen in any system that mixes priorities with locks.
The three-task scenario
Three tasks, three priorities: High (H), Medium (M), Low (L).
- L starts, takes a mutex, and begins its work inside the critical section.
- M becomes ready. It has higher priority than L, so it preempts L. L still holds the mutex.
- H becomes ready and tries to take the same mutex. It blocks, because L holds it.
- Now L cannot run (M is running) and H cannot run (it waits on L). M, which should be less important than H, effectively runs ahead of H for as long as it wants.
H's wait is no longer bounded by L's short critical section. It is bounded by however long M runs, and M has nothing to do with the lock.
The fix: priority inheritance
When H blocks on a mutex held by L, the kernel temporarily raises L to H's priority. Now M cannot preempt L. L finishes the critical section, releases the mutex, drops back to its own priority, and H runs. H's wait is bounded by the length of L's critical section, as designed. Most RTOS mutexes (FreeRTOS mutexes, Zephyr mutexes, ThreadX mutexes with the right option) support inheritance. FreeRTOS binary semaphores do not, which is a common trap.
See it in a simulation
This program simulates a one-tick-per-step scheduler. L arrives at time 0, M at time 1, H at time 2. L needs 4 ticks of work in its critical section, H needs 2 ticks and takes the mutex first, M needs 5 ticks and never touches the mutex.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
enum { L, M, H, NT };
static const char NAME[NT] = {'L', 'M', 'H'};
static const int PRIO[NT] = {1, 2, 3};
static const int ARRIVE[NT] = {0, 1, 2};
static const int WORK[NT] = {4, 5, 2};
static const bool USES_MUTEX[NT] = {true, false, true};
/* returns H's response time (arrival to completion); fills timeline */
static int run(bool inherit, char *timeline)
{
int left[NT], t, done_h = -1;
int mutex_owner = -1;
bool finished[NT] = {false, false, false};
for (int i = 0; i < NT; i++) left[i] = WORK[i];
for (t = 0; t < 40 && done_h < 0; t++) {
int eff[NT], pick = -1;
for (int i = 0; i < NT; i++) eff[i] = PRIO[i];
/* a task is blocked if it needs the mutex and someone else holds it */
bool blocked[NT] = {false, false, false};
for (int i = 0; i < NT; i++)
if (USES_MUTEX[i] && mutex_owner >= 0 && mutex_owner != i && ARRIVE[i] <= t && !finished[i] && left[i] == WORK[i])
blocked[i] = true;
if (inherit && mutex_owner >= 0)
for (int i = 0; i < NT; i++)
if (blocked[i] && PRIO[i] > eff[mutex_owner]) eff[mutex_owner] = PRIO[i];
for (int i = 0; i < NT; i++) {
bool ready = ARRIVE[i] <= t && !finished[i] && !blocked[i];
if (ready && (pick < 0 || eff[i] > eff[pick])) pick = i;
}
timeline[t] = pick < 0 ? '.' : NAME[pick];
if (pick < 0) continue;
if (USES_MUTEX[pick] && mutex_owner < 0) mutex_owner = pick;
if (--left[pick] == 0) {
finished[pick] = true;
if (pick == mutex_owner) mutex_owner = -1;
if (pick == H) done_h = t + 1;
}
}
timeline[t] = '\0';
return done_h - ARRIVE[H];
}
int main(void)
{
char a[64], b[64];
int plain = run(false, a), inh = run(true, b);
printf("no inheritance : %-14s H response = %d ticks\n", a, plain);
printf("with inheritance : %-14s H response = %d ticks\n", b, inh);
return (inh < plain) ? 0 : 1;
}
no inheritance : LMMMMMLLLHH H response = 9 ticks with inheritance : LMLLLHH H response = 5 ticks
Compiled with -Wall -Wextra -Werror and run under AddressSanitizer + UBSan.
Read each timeline as one letter per tick: which task ran. Without inheritance, M runs for five ticks while H waits, and H's response time is the long one. With inheritance L runs right after H blocks, releases the mutex, and H goes next.
Learning this for work or an interview? Lessons 1 and 2 of DSA for Firmware are free, with labs and C exercises. Start Lesson 1
Other ways to avoid it
- Priority ceiling protocol. Give each mutex a ceiling equal to the highest priority of any task that may take it, and raise the holder to that ceiling immediately.
- Shorter critical sections. Do the minimum while holding the lock.
- No lock at all. For a single producer and a single consumer, a lock-free ring buffer removes the shared lock.
- Do not share between very different priorities. If you can, give the data to one owner task and send it messages.
What to say in an interview
Name the three tasks, say that the middle one starves the high one while the low one holds the lock, name priority inheritance as the standard fix and priority ceiling as the alternative, and mention which of your RTOS primitives actually implement it.
Read the scheduler timeline
A task can be ready to run, running, or blocked. A high-priority task blocked on a mutex is not ready, so the scheduler may legitimately run a lower-priority ready task. The problem appears when that ready task delays the mutex owner. Priority inheritance temporarily raises the owner's effective priority; it does not change the priorities permanently.
| Observation | What to inspect |
|---|---|
| High task is blocked | Which task owns its mutex? |
| Medium task keeps running | Can it preempt the owner? |
| Owner runs but remains blocked | Does it depend on another resource? |
| Mutex is released | Does the kernel restore the owner's effective priority? |
Interview check: mutex versus semaphore
A mutex has an owner and may support inheritance. A binary semaphore is often an event-signaling mechanism and need not have an owner. Do not assume either primitive has inheritance without checking your RTOS. Inheritance does not eliminate deadlocks, long critical sections or unrelated higher-priority work. Review bounded mutex use and practice the free firmware interview questions.
DSA for Firmware teaches it one real bug at a time.
- 28 lessons, each starting from a real firmware bug.
- 110 C exercises with tests, and a reference solution for every one.
- Lessons 1 and 2 are free. No sign-up.
Get the next article by email
One short email when a new article or free lesson is published. No spam. Unsubscribe at any time by replying to any email. See the privacy policy.
Keep reading
- Priority inversion: review shared-resource wait times
- What
volatilereally does in C, and what it does not - A ring buffer in C for a UART interrupt, step by step
- Firmware and embedded C interview questions, with answers
- RTOS fundamentals for firmware engineers
- A practical firmware interview preparation roadmap
- All articles