Priority inversion is a problem in real-time systems, and occurs only when using a priority-based preemptive kernel. Figure 13-4 illustrates a priority-inversion scenario. Task H (high priority) has a higher priority than Task M (medium priority), which in turn has a higher priority than Task L (low priority).F13-4
(1)Task H and Task M are both waiting for an event to occur and Task L is executing.
F13-4(2)At some point, Task L acquires a semaphore, which it needs before it can access a shared resource. F13-4
(3)Task L performs operations on the acquired resource.
F13-4(4)The event that Task H was waiting for occurs, and the kernel suspends Task L and start executing Task H since Task H has a higher priority. F13-4
(5)Task H performs computations based on the event it just received.
F13-4(6)Task H now wants to access the resource that Task L currently owns (i.e., it attempts to get the semaphore that Task L owns). Because Task L owns the resource, Task H is placed in a list of tasks waiting for the semaphore to be available. F13-4
(7)Task L is resumed and continues to access the shared resource.
F13-4(8)Task L is preempted by Task M since the event that Task M was waiting for occurred. F13-4
(9)Task M handles the event.
F13-4(10)When Task M completes, the kernel relinquishes the CPU back to Task L. F13-4
(11)Task L continues accessing the resource.
F13-4(12)Task L finally finishes working with the resource and releases the semaphore. At this point, the kernel knows that a higher-priority task is waiting for the semaphore, and a context switch takes place to resume Task H. F13-4
(13)Task H has the semaphore and can access the shared resource.
...