Embedded Software Interview – Operating Systems: 1 – What is Priority Inversion and How to Resolve It

1. What is Priority Inversion?

A high-priority task is indirectly blocked by a low-priority task, causing a medium-priority task to execute before the high-priority task, which violates the original intention of priority scheduling.

2. Example Illustration

Assume there are three tasks:

  • High Priority Task (High)
  • Medium Priority Task (Medium)
  • Low Priority Task (Low)

The process is as follows:

  1. The Low task acquires a shared resource (such as a mutex) and is running.
  2. At this time, the High task needs that resource, attempts to acquire the lock, and is blocked (waiting for Low to release the resource).
  3. While Low has not released the lock, the Medium task starts running (with a higher priority than Low), preempting the CPU.
  4. Since Medium does not depend on the resource, it can run continuously, causing the Low task to be unable to obtain CPU time to release the lock, and High remains blocked.

Result:The High task, which has the highest priority, is blocked by the Medium and Low tasks, losing the real-time guarantee of its high priority.

3. Solutions

a. Priority Inheritance

  • When a low-priority task holds a resource and blocks a high-priority task,temporarily elevate the priority of the low-priority task to that of the blocked high-priority task..
  • This way, the Medium task will not preempt Low, allowing Low to finish running and release the resource, then High can acquire the resource and continue execution.
  • After releasing the resource, Low returns to its original priority.
b. Priority Ceiling Protocol)
  • Assign a “priority ceiling” to each resource, which is the highest priority among tasks that can access that resource.
  • When a task acquires a resource, it is temporarily elevated to the ceiling priority of that resource.
  • This prevents other medium-priority tasks from preempting, avoiding inversion.
c. Prohibit Medium Priority Task Preemption (Applicable to Small Systems)
  • When acquiring critical resources, temporarily prohibit task switching or interrupts (not recommended for large systems).

Leave a Comment