0
votes

Both the book "Operating System Principles" by Silberschatz/Galvin (9th Edition), as well as the article on Wikipedia on priority inversion say that in priority inversion, a high priority process can't use a resource held by a low priority process as a medium priority process preempts the low priority process and gets that resource. But if so (i.e., if priority-based preemption is allowed) why can't the high priority process preempt the lower priority process itself and get the resource?

Below is what is mentioned in the book as well as the Wikipedia article:

Consider two tasks H and L, of high and low priority respectively, either of which can acquire exclusive use of a shared resource R. If H attempts to acquire R after L has acquired it, then H becomes blocked until L relinquishes the resource. Sharing an exclusive-use resource (R in this case) in a well-designed system typically involves L relinquishing R promptly so that H (a higher priority task) does not stay blocked for excessive periods of time. Despite good design, however, it is possible that a third task M of medium priority (p(L) < p(M) < p(H), where p(x) represents the priority for task (x)) becomes runnable during L's use of R. At this point, M being higher in priority than L, preempts L, causing L to not be able to relinquish R promptly, in turn causing H—the highest priority process—to be unable to run. This is called priority inversion where a higher priority task is preempted by a lower priority one.

1

1 Answers

1
votes

Here is a better example which will help you understand. I will at the end explain and answer your question.

Let there be 3 processes of different priorities. Low, Med and High. (Low, Med and high in terms of their priority).

Let the Low and High processes access the same critical resource at different times.

The Lowest priority process is running, Med and High are blocked and out of their critical section.

Low enters the critical section and aquires the resource needed by High.

High unblocks and since it is the highest priority task in the system, it runs.

High then attempts to enter the critical resource but blocks as Low is in there.

Med unblocks and since it is now the highest priority task in the system, it runs.

High can not run until Low gives up the resource. Low can not run until Med blocks or ends. The priority of the tasks has been inverted; High though it has the highest priority is at the bottom of the execution chain.

To "solve" priority inversion, the priority of Low must be bumped up to be at least as high as High.

Bottom line: Med caused High to wait. The critical section is protected by a synchronization tool, the high priority process won't preempt the shared resource up until the low priority process voluntarily gives it up.

But the low priority process needs a resource which is currently in use by the Med process. => High priority process has to wait.

Read this: What is priority inversion?

Credit to the guy that wrote the example in one of the comments.