Home / Learn Embedded C / Priority Inversion and How to Avoid It
Priority Inversion and How to Avoid It
A classic RTOS pitfall where a low-priority task unintentionally blocks a high-priority one.
The idea, explained simply
The basic idea
Priority inversion happens when a low-priority task holds a mutex a high-priority task needs, and a medium-priority task (unrelated to the mutex) keeps preempting the low-priority task — the high-priority task ends up waiting far longer than its priority should ever allow.
Why this comes up
This is a famous, real-world RTOS bug class (notably involved in a well-documented Mars Pathfinder mission issue) — understanding it is essential because it can silently violate the entire point of having task priorities in the first place.
The mechanics
Priority inheritance is the standard fix: while a low-priority task holds a mutex that a higher-priority task is waiting for, the low-priority task is temporarily boosted to the waiting task's priority, preventing medium-priority tasks from indefinitely preempting it in between.
Where it is used
- RTOS reliability
- Real-time guarantee analysis
- Famous historical embedded bugs
Try it
The boosted_low_priority line is exactly what priority inheritance does: temporarily raising the mutex holder above the medium-priority interferer.
Try it: counting how many times the medium task preempts
Each preemption by the medium-priority task adds directly to how long the high-priority task is stuck waiting — a concrete way to see the inversion's cost.
Quick check
1. What is priority inheritance meant to prevent?
Priority inheritance temporarily raises the mutex-holding low-priority task's priority, preventing an unrelated medium-priority task from indefinitely delaying the high-priority task waiting on that mutex.