In this lesson, you will learn how a medium-priority task can indirectly block a high-priority task, subverting your real-time scheduling guarantees.
The Mechanics of Priority Inversion
Priority inversion occurs when a high-priority task (Task_H) is forced to wait for a low-priority task (Task_L) to release a shared resource, while an unrelated medium-priority task (Task_M) preempts the low-priority task, effectively starving the high-priority task indefinitely.
The Execution Flow
Consider a shared resource (like a hardware peripheral or data buffer) protected by a mutex. The failure sequence unfolds as follows:
- Task L acquires the resource:
Task_L(Low Priority) locks the mutex. - Task H preempts Task L:
Task_H(High Priority) wakes up and starts executing. - Task H requests the resource:
Task_Hattempts to lock the mutex held byTask_L. It is blocked and enters aBlockedstate. The scheduler switches back toTask_L. - Task M preempts Task L: Suddenly,
Task_M(Medium Priority) becomes ready. SinceTask_Mhas a higher priority thanTask_L, the scheduler preemptsTask_L. - The Violation:
Task_Mexecutes whileTask_Lremains preempted. BecauseTask_Lcannot run, it cannot reach the code to release the mutex.Task_His now indirectly waiting forTask_Mto finish, even though they share no common resource.
// Conceptual flow:
// 1. Task_L locks(mutex)
// 2. Task_H preempts, tries to lock(mutex) -> BLOCKED
// 3. Task_L resumes
// 4. Task_M preempts Task_L -> Task_H is now stuck behind Task_M!
Why This Destroys Determinism
In a real-time system, Task_H is designed to meet strict deadlines. When Task_M executes in place of Task_L, the execution time of Task_H becomes dependent on the execution time of Task_M. If Task_M performs long computations, Task_H will miss its deadline. This violation occurs because the system’s scheduling logic no longer respects the intended priority hierarchy; the medium-priority task effectively "steals" the execution time reserved for the high-priority task's dependencies.
Key Takeaway: Priority inversion is a failure of system determinism where a medium-priority task creates an unbounded delay for a high-priority task by preempting the low-priority task currently holding a required resource.