What is priority inversion?

Priority inversion occurs when a lower-priority process takes precedence over a higher-priority process. This means that the priorities of the two processes are reversed. This is the most occurring problem in the case of a busy-waiting strategy. This problem occurs when there is already a process in a section of the CPU called the critical sectionThe critical section refers to the segment of code where processes access shared resources, such as common variables and files, and perform write operations on them. .

Note: Learn more about the critical section by clicking here.

When one process is executing in its critical section, no other process is allowed to enter the critical section. This is called mutual exclusion.

Mutual exclusion

In mutual exclusion, only a single process is executed in the critical section at a time. We need a CPU to execute a process, which is a resource used to execute the critical part of the code. The problem occurs when another process with a high priority tries to enter the critical section. In this case, we pre-empt the resources from the process that is already running in the critical section so that we can use those resources for the execution of the new incoming process.

However, when we pre-empt the resources from the prior process, it will stop in the critical section and cannot go out of that because it has no CPU to execute the remaining part of the process. While the new incoming process will also stay outside of the critical section because the entry section will not allow it to enter the critical section. This is because there is already a process stuck in the critical section.

This is the principle of mutual exclusion. Even though the priority of a new process is higher than the previous one, it will stay outside the critical section until the critical section is free to execute the process. So both processes are stuck, one inside the critical section and the other outside the entry section. This situation is called deadlock or priority inversion.

It is illustrated below:

Priority inversion
Priority inversion

This is a basic process synchronization strategy. Here, process P1 is already running in the critical section. When a new process P0 tries to enter the critical section, the resources are taken away from P1 as the priority of P0 is higher than P1, and the CPU will be given to P0. P1 was in the critical section, and the CPU has been snatched from it, so it cannot execute the remaining code and will be stuck there. Also, the entry section will not allow P0 to enter because there’s already a process in the critical section.

In this situation, both are locked at their positions, and a high-priority task is blocked because a lower-priority task is already running in the critical section. This situation is called a priority inversion problem because a lower-priority task is running before the higher-priority task, and their priorities have been inverted. Now, as the lower-priority task has kept the higher-priority task on wait, this is called bounded priority inversion.

P1 has the critical section
1 of 5

In the third slide, when there is a high-priority task waiting for a low- priority task, then the bounded inversion situation occurs.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved