EmbedwrightArticlesLesson guideFree lessonsThe course · $79

Priority inversion: what it is and how inheritance fixes it

By Chinmay Gupta · Updated 2026-12-03
Short answer. Priority inversion is when a high-priority task waits for a lock held by a low-priority task, and a medium-priority task keeps the low one from running. Priority inheritance fixes it by temporarily raising the lock holder to the waiter's priority.

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).

  1. L starts, takes a mutex, and begins its work inside the critical section.
  2. M becomes ready. It has higher priority than L, so it preempts L. L still holds the mutex.
  3. H becomes ready and tries to take the same mutex. It blocks, because L holds it.
  4. 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;
}
Real output
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

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.

ObservationWhat to inspect
High task is blockedWhich task owns its mutex?
Medium task keeps runningCan it preempt the owner?
Owner runs but remains blockedDoes it depend on another resource?
Mutex is releasedDoes 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.

The course goes further on this topic. Lesson 24 of DSA for Firmware, “The counter that lost count”, adds interactive labs and C exercises with tests that you run on your own machine. Read the lesson guide · See the course
Want the full path from “I know C” to “I can choose the right data structure under a RAM budget”?
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.
Start Lesson 1 freeSee the full course · $79

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.

Chinmay Gupta
Chinmay Gupta is a firmware engineer with more than eight years of experience writing and debugging embedded C. He builds Embedwright's DSA for Firmware course.