Course contentsShow
← Linux
Lesson 474 of 1,8195. Process Management and SignalingFree lesson

Priority Inheritance and Scheduling Policies

Explain how the scheduler handles resource contention between processes with varying priorities.

In this lesson, you will learn how the Linux scheduler prevents "priority inversion" by allowing high-priority processes to temporarily boost the priority of low-priority processes holding a required resource.

The Problem: Priority Inversion

Imagine you have three processes: High, Medium, and Low.

  1. Low acquires a shared resource (like a mutex lock) that High also needs.
  2. While Low is holding the lock, Medium starts running. Because Medium has a higher priority than Low, the scheduler preempts Low to give CPU time to Medium.
  3. Now, High is blocked waiting for the resource, but it cannot proceed because Low (the lock holder) isn't getting any CPU time to finish its task and release the lock.

Even though High is the most important process, it is effectively stuck behind Medium, which doesn't even need the resource. This is priority inversion.

The Solution: Priority Inheritance

To fix this, Linux employs Priority Inheritance. When the kernel detects that a high-priority process is blocked waiting for a lock held by a lower-priority process, it temporarily "inherits" the priority of the high-priority process to the low-priority one.

By boosting the priority of Low to match High, the scheduler ensures Low runs immediately, finishes its work, and releases the resource. Once the lock is released, the kernel restores Low to its original, lower priority level, allowing the high-priority process to finally acquire the lock and proceed.

Observing Scheduling Policies

Linux uses different scheduling policies to manage these priorities. While standard processes use SCHED_OTHER (based on nice values), real-time processes use SCHED_FIFO or SCHED_RR. You can inspect the current policy and priority of a process using:

# Check the scheduling policy and priority of a process
chrt -p <PID>

The scheduler uses these policies alongside priority inheritance to ensure that critical system tasks remain responsive, even when they rely on shared data protected by lower-priority processes.

Key Takeaway: Priority inheritance solves resource contention deadlocks by temporarily elevating the priority of a low-priority process to prevent it from blocking a high-priority process.