Priority inversion and priority inheritance
About this lesson
A preemptive RTOS always runs the most urgent ready task, until a lock gets in the way. Three FreeRTOS tasks on one core share an I2C bus: motor (priority 3, with a deadline), radio (2) and log (1). Watch the timeline as log holds the lock, motor waits for it, and radio, which needs no lock at all, runs in between: motor ends 5 ms late, the bug that reset Mars Pathfinder in 1997. Then replay it with a FreeRTOS mutex, whose priority inheritance keeps the wait short. Semaphores, locks and real-time scheduling in general are taught in the Operating Systems course; the FreeRTOS mutex API itself has its own lesson in this course.
Three tasks, one CPU
One core runs three FreeRTOS tasks. motor has priority 3, radio 2, log 1; a bigger number is more urgent. The scheduler always runs the most urgent task that is ready. Each tick is 1 ms.
priority bigger number = more urgent (0 is the idle task) motor 3 drives the motor; must finish 6 ms after it wakes radio 2 sends a radio packet; never touches the bus log 1 reads a sensor on the I2C bus for the log rule run the highest-priority task that is ready tick 1 ms (configTICK_RATE_HZ 1000)
Read the lesson step by step
Three tasks, one CPU
One core runs three FreeRTOS tasks. motor has priority 3, radio 2, log 1; a bigger number is more urgent. The scheduler always runs the most urgent task that is ready. Each tick is 1 ms.
priority bigger number = more urgent (0 is the idle task) motor 3 drives the motor; must finish 6 ms after it wakes radio 2 sends a radio packet; never touches the bus log 1 reads a sensor on the I2C bus for the log rule run the highest-priority task that is ready tick 1 ms (configTICK_RATE_HZ 1000)log takes the lock
At 0 ms only log is ready, so it runs. It calls xSemaphoreTake and gets the lock, then starts a 3 ms read on the I2C bus. Our lock is a binary semaphore: a lock with no extra rules.
t = 0 ms log runs (only ready task) line 2 xSemaphoreTake(lock, LIMIT) -> lock is free: log holds it line 3 readSensor() needs 3 ms on the bus lock lock = xSemaphoreCreateBinary();motor preempts log
At 1 ms motor wakes. 3 beats 1, so the scheduler stops log at once and runs motor. That is preemption. log keeps the lock while it waits: it is 1 ms into its 3 ms read.
t = 1 ms motor wakes: priority 3 > 1 log -> Ready (still holds the lock) motor -> Running deadline motor must finish by 7 msmotor waits for the lock
At 2 ms motor needs the bus and calls xSemaphoreTake, but log holds the lock. motor is blocked, so log runs again. A short wait like this is normal: at most log's 3 ms read.
t = 2 ms motor: xSemaphoreTake(lock) -> held by log motor -> Blocked (waits for the lock) log -> Running: the only ready task bounded worst wait = log's critical section, 3 msradio jumps the queue
At 3 ms radio wakes. 2 beats log's 1, so radio runs. Now motor waits for log, and log waits for radio. A task that needs no lock is delaying the most urgent one. This is priority inversion.
t = 3 ms radio wakes: priority 2 > 1 radio -> Running log -> Ready (holds the lock, cannot run) motor -> Blocked on the lock in effect motor (3) is waiting for radio (2)The deadline passes
radio runs for 6 ms, and motor's deadline at 7 ms goes by. Nothing limits this wait: a longer radio job, or more medium tasks, would stretch it further. That is called unbounded priority inversion.
radio runs 3 -> 9 ms (6 ms) deadline 7 ms -> missed unbounded the wait depends on every task between H and L Mars Pathfinder, 1997: the same pattern, a low weather task, a medium radio task and a high bus taskmotor ends 5 ms late
radio finishes, log ends its read and gives the lock at 10 ms. motor finally takes it and finishes at 12 ms: 5 ms late. On Mars Pathfinder in 1997, a watchdog saw such a late task and reset the whole computer.
t = 10 ms log: xSemaphoreGive(lock) -> motor gets it t = 12 ms motor done late 12 - 7 = 5 ms past the deadline timeline LHLMMMMMMLHHReplay with a mutex
Same tasks, but the lock is now a FreeRTOS mutex. At 2 ms motor blocks on it as before. This time FreeRTOS lifts log to motor's priority, 3, while log holds the lock. That is priority inheritance.
line 1 xSemaphoreCreateMutex(): a mutex inherits, a binary semaphore does not t = 2 ms motor blocks on the mutex held by log log's priority 1 -> 3 (inherited from motor) needs configUSE_MUTEXES 1radio has to wait
At 3 ms radio wakes again, but 2 no longer beats log, which now runs at 3. log keeps the CPU and finishes its read at 4 ms. motor's wait is now bounded: only log's short read.
t = 3 ms radio wakes: priority 2 < log's inherited 3 radio -> Ready (waits) log keeps running its read t = 4 ms log's read is doneGive back, drop back
log gives the mutex at 4 ms and drops back to priority 1. motor takes it and finishes at 6 ms, 1 ms before its deadline. Then radio runs. Same 12 ms of work, in a better order.
t = 4 ms log: xSemaphoreGive(lock) -> priority back to 1 motor takes the mutex, done at 6 ms (deadline 7) radio runs 6 -> 12 ms binary LHLMMMMMMLHH motor done at 12 mutex LHLLHHMMMMMM motor done at 6Rules for shared locks
Use a mutex, not a binary semaphore, to guard a shared part. Keep the code between take and give short. Give a timeout to xSemaphoreTake. Never take a mutex inside an interrupt. Inheritance limits the wait; it does not remove it.
guard with xSemaphoreCreateMutex() keep short the code between take and give time out xSemaphoreTake(lock, LIMIT), not forever no ISRs mutexes cannot be used in interrupts bounded inheritance limits the wait, it does not remove it
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In FreeRTOS, which task does the scheduler run?
A bigger priority number means more urgent, and the scheduler always runs the most urgent task that is able to run. Tasks of equal priority can share time, but a lower-priority task never runs while a higher one is ready.
02log (priority 1) holds the lock. motor (priority 3) needs it and blocks. Then radio (priority 2) wakes up. With a binary semaphore, what happens?
2 beats 1, so radio runs. log cannot finish and give the lock back, so motor waits for a task that does not even use the lock. That is unbounded priority inversion: in our example motor ends 5 ms late.
03What does priority inheritance do?
While motor waits, log runs at priority 3, so radio cannot cut in. log drops back to 1 as soon as it gives the mutex, and motor finishes at 6 ms, before its deadline at 7 ms.
04Which FreeRTOS object should guard an I2C bus shared by several tasks?
FreeRTOS mutexes include priority inheritance and binary semaphores do not. A bool check-then-set is not atomic: a task can be preempted between reading it and setting it, and waiting tasks must spin. Mutexes cannot be used from interrupt service routines at all.
05Does priority inheritance remove priority inversion completely?
The FreeRTOS kernel book says inheritance does not fix priority inversion; it only makes sure the inversion is time bounded. The high task can still wait for the low task's critical section, so keep it short and give xSemaphoreTake a timeout.
Glossary
- Task priority
- A number that says how urgent a FreeRTOS task is. A bigger number is more urgent; 0 is the idle task.
- Preemption
- The scheduler stops a running task at once when a more urgent task becomes ready, and runs that one instead.
- Critical section
- The code between taking a lock and giving it back, where a task uses the shared part.
- Mutex
- A lock that one task takes and the same task gives back. In FreeRTOS it includes priority inheritance.
- Binary semaphore
- A FreeRTOS signal that is either available or not. It can act as a lock but has no priority inheritance; it suits signalling, for example from an ISR.
- Priority inversion
- A high-priority task waits while lower-priority tasks run. It is unbounded when tasks that do not use the lock can make the wait as long as they like.
- Priority inheritance
- The lock holder temporarily runs at the priority of the most urgent task waiting for the lock, then drops back when it gives the lock.
- Deadline
- The time by which a real-time task must finish. A late answer counts as a wrong one.
Notes and sources
- A binary semaphore made with xSemaphoreCreateBinary() starts empty: it must be given once before anyone can take it. Used as a lock it works, but it has no priority inheritance. That is why FreeRTOS tells you to guard shared parts with a mutex.
- The task lengths are our example, chosen to make the effect easy to see, on an STM32F407 (single-core Arm Cortex-M4) with a 1 ms tick. readSensor() and sendPacket() stand for real I2C and radio drivers. With one core only one task can run at a time; dual-core chips such as the ESP32 add more cases.
- Mars Pathfinder ran the VxWorks RTOS, not FreeRTOS. A low-priority weather task held the bus lock, a medium-priority radio task ran, and the high-priority bus task missed its deadline; a watchdog then reset the computer. Engineers fixed it from Earth by turning on the lock's priority inheritance flag.
- FreeRTOS uses a basic form of inheritance to save time and memory. A task that holds several mutexes keeps its highest inherited priority until it has given all of them back. The kernel book also warns not to rely on inheritance for correct timing: design the locking so long waits cannot happen.
- Sha, Rajkumar and Lehoczky also described the priority ceiling protocol: each lock gets a ceiling (the highest priority of any task that uses it), and a task may take a lock only if its priority is above the ceilings of all locks other tasks hold. That also prevents deadlock. FreeRTOS mutexes use plain inheritance instead.
- R. Barry et al., Mastering the FreeRTOS Real Time Kernel, ch. 8 Resource Management (§8.3 priority inversion and inheritance)
- R. Barry et al., Mastering the FreeRTOS Real Time Kernel, ch. 4 Task Management (priorities and the tick)
- FreeRTOS-Kernel source: semphr.h (xSemaphoreCreateMutex, xSemaphoreCreateBinary)
- FreeRTOS documentation: Mutexes
- E. A. Lee and S. A. Seshia, Introduction to Embedded Systems (v2.3), §12.4 Scheduling and mutual exclusion (UC Berkeley EECS 149)
- L. Sha, R. Rajkumar, J. P. Lehoczky, Priority inheritance protocols, IEEE Transactions on Computers 39(9), 1990
- M. Jones, What really happened on Mars? (Mars Pathfinder, 1997)
Quiz Unlocks after the last step.