What is Priority inversion?

A situation in which a high-priority task waiting for a resource held by a low-priority task is held up because a medium-priority task preempts the low one.

Field: Embedded Systems & Firmware

Japanese name: 優先度逆転

How priority inversion happens

Consider three tasks in a preemptive real-time operating system (RTOS): high (H), medium (M) and low (L) priority. L locks a mutex protecting a shared resource. H becomes ready, needs the same mutex and blocks, waiting for L to release it. Now M, which has nothing to do with the resource, becomes ready. Because M outranks L, the scheduler runs M and L cannot reach the point where it releases the mutex. As long as M keeps running, H waits as well, so in effect a medium-priority task is delaying the highest-priority task, inverting the intended order.

Bounded vs. unbounded inversion

Some blocking is unavoidable whenever tasks share a resource: if L holds the mutex, H must wait for L to finish its critical section. That delay is bounded by the length of the critical section and can be included in timing analysis. The problem is the medium-priority interference, which makes the delay unbounded, since any number of medium tasks can run in between. In a hard real-time system, where deadlines must be guaranteed, an unbounded delay can mean a missed deadline, a watchdog reset or unsafe behavior, which is why the issue receives so much attention.

Priority inheritance and priority ceiling

Priority inheritance fixes the problem dynamically: when H blocks on a mutex held by L, L temporarily takes on H's priority, so M cannot preempt it. L finishes its critical section, releases the mutex, drops back to its own priority, and H runs. Many RTOS mutexes provide this option. The priority ceiling protocol is a static alternative: each mutex is assigned the priority of the highest task that may use it, and any task locking it is raised to that ceiling immediately. Ceiling protocols also prevent certain deadlocks, at the cost of analyzing which tasks use which resources.

The Mars Pathfinder incident

A well-known example occurred on the Mars Pathfinder lander. After landing, the spacecraft suffered repeated system resets. A high-priority task was waiting for a mutex held by a low-priority task, which was in turn preempted by medium-priority work, until the system's own monitoring noticed that a periodic task had not completed in time and reset the system. Engineers reproduced the problem on a ground replica and fixed it by enabling priority inheritance on the mutex involved. Keeping critical sections short and minimizing resources shared across priority levels are useful design rules.

Try it: questions about Priority inversion

Which describes priority inversion in an RTOS?

  1. AWhile a high-priority task waits for a resource held by a low-priority task, a medium-priority task preempts the low one and keeps running, so the high task waits a long time
  2. BA bug where priority numbers are interpreted backwards
  3. CA rule that high-priority tasks always run after low-priority ones
Show answer and explanation

Answer: A. While a high-priority task waits for a resource held by a low-priority task, a medium-priority task preempts the low one and keeps running, so the high task waits a long time

If a high-priority task waits for a mutex held by a low-priority task, it must wait until the low task releases it. If an unrelated medium-priority task then starts running, it preempts the low task, which cannot release the resource, so in effect the high task is held up by the medium task. On the Mars Pathfinder lander, this caused repeated system resets.

Which describes priority inheritance, used to prevent priority inversion?

  1. ALower the high-priority task to the low task's priority
  2. BGive all tasks the same priority
  3. CTemporarily raise the low-priority task holding a resource to the priority of the high-priority task waiting for it
Show answer and explanation

Answer: C. Temporarily raise the low-priority task holding a resource to the priority of the high-priority task waiting for it

With priority inheritance, when a high-priority task starts waiting for a mutex held by a low-priority task, the low task temporarily gets the high task's priority. It then proceeds without being preempted by medium tasks and returns to its original priority on releasing the resource. The high task's wait is then roughly bounded by the time the low task uses the resource, although nested mutexes can chain the waits and make them longer. Many RTOS mutexes provide this feature.

FAQ

What is priority inversion in an RTOS?

It is a situation in which a high-priority task waits for a resource held by a low-priority task, and a medium-priority task preempts the low one, so the high-priority task is effectively delayed by the medium one. The delay can become unbounded.

What is the difference between priority inheritance and priority ceiling?

Priority inheritance raises a lock holder's priority only when a higher-priority task actually blocks on that lock. Priority ceiling raises any task to a preset ceiling priority as soon as it takes the lock, which also helps prevent deadlocks.

How was priority inversion fixed on Mars Pathfinder?

The repeated resets were traced to priority inversion on a shared mutex, and engineers fixed them by enabling priority inheritance on that mutex in the flight software.

Books on Embedded Systems & Firmware Ad

Learn hands-on: parts and kits

Building real circuits is a good way to check what you learned in the questions.

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary