Priority scheduling

On this page 7

Priority scheduling is a CPU scheduling policy in which the operating system always dispatches the ready process or thread with the highest priority, and lower-priority work runs only when nothing more important is waiting. In operating systems, it is the core idea behind the schedulers of Windows, Linux real-time classes and most embedded kernels, and it sits at the center of processes & scheduling alongside round-robin and fair-share policies.

A priority is a number attached to each schedulable entity that expresses its importance or urgency. The scheduler compares these numbers at every scheduling decision, so priority scheduling directly determines responsiveness, throughput and the risk of starvation.

How priority scheduling selects the next task

The scheduler keeps ready tasks in a structure ordered by priority, typically one run queue per priority level plus a bitmap that marks which levels are non-empty. At each decision point it finds the highest non-empty level and picks the first task there; tasks that share a level are usually served in round-robin order with a time slice.

Scheduling decisions occur when a running task blocks, exits, yields, or when a higher-priority task becomes ready. In a preemptive variant, a newly ready task with higher priority immediately displaces the running task. In a non-preemptive variant, it waits until the current task gives up the CPU voluntarily.

The table contrasts the two variants and their consequences.

PropertyPreemptiveNon-preemptive
When a higher priority arrivesRunning task is interruptedNew task waits for a yield
Response time for urgent workShort and boundedDepends on current task length
Context switch overheadHigherLower
Typical useDesktop, server and real-time kernelsSimple embedded loops, early systems
Preemptive and non-preemptive priority scheduling

Static and dynamic priorities

A static priority is set when a task is created or configured and does not change unless an administrator or the program changes it. A dynamic priority is adjusted by the kernel at run time, usually to reward interactive, I/O-bound tasks and to keep CPU-bound tasks from monopolizing the processor.

General-purpose systems combine both: the user sets a base priority, and the scheduler applies boosts or decay around it. The table lists common ways priorities are assigned.

Priority sourceHow it is setExample
Administrative settingUser or administrator commandThe nice value on Unix-like systems
Priority classProcess creation flag or API callWindows priority classes
Kernel boostTemporary raise after an eventForeground window or I/O completion boost
AgingGradual raise while waitingLong-waiting task climbs levels
Deadline or rateDerived from timing requirementsRate-monotonic assignment
Sources of task priority

Priority numbering is not consistent across systems. On Linux, a lower nice value means higher priority, while real-time priorities run from 1 to 99 with 99 the highest. Windows uses 32 levels, where 16-31 form the real-time range. Reading priority values without knowing the convention is a frequent source of configuration errors.

Starvation, aging and priority inversion

The main weakness of strict priority scheduling is starvation: if higher-priority tasks keep arriving, a low-priority task may never run. The classic remedy is aging, which raises the priority of a task the longer it waits, guaranteeing that it eventually reaches the CPU.

A second hazard is priority inversion. A high-priority task blocks on a lock held by a low-priority task, while a medium-priority task preempts the lock holder, so the urgent task waits on work that is not even related to it. Kernels counter this with priority inheritance, which temporarily lifts the lock holder to the waiting task’s priority, or with priority ceiling protocols.

The table maps typical symptoms to their cause and the standard fix.

SymptomCauseStandard remedy
Background job never finishesStarvation by higher levelsAging or minimum CPU share
Urgent task misses deadline on a lockPriority inversionPriority inheritance mutexes
System becomes unresponsiveRunaway real-time taskReal-time throttling or watchdog
Interactive lag under loadCPU-bound tasks at equal priorityDynamic boosts for I/O-bound tasks
Common priority scheduling problems

Several classic policies can be expressed as special cases of priority scheduling. Shortest job first is priority scheduling where priority is the inverse of the predicted burst length, and first-come, first-served corresponds to priority by arrival time. A multilevel feedback queue is a dynamic priority scheduler that moves tasks between levels based on observed behavior.

Real-time policies refine the idea. Rate-monotonic scheduling assigns fixed priorities by period, while earliest deadline first recomputes priority from the nearest deadline. Fair-share schedulers, such as the Linux Completely Fair Scheduler and its successor EEVDF, use priorities as weights for CPU share rather than absolute precedence, so low-weight tasks still progress.

  • Strict priority: the highest level always wins; predictable but prone to starvation.
  • Weighted fair share: priority changes the proportion of CPU time, not the order.
  • Deadline-based: priority follows timing constraints and can change every period.

Configuring priorities in practice

The decisive rule is to raise priority only for work that is short and latency-sensitive. A high-priority task that runs long or loops without blocking will degrade everything below it, and on real-time classes it can freeze the user interface entirely. Lowering the priority of batch work is usually safer than raising the priority of interactive work.

Measurement should precede tuning: tools that show run-queue latency and per-thread CPU time reveal whether a task is actually waiting for the CPU or for I/O. Changing priority does not help an I/O-bound task that is waiting on disk or network.

Stripped to the essentials

A computer has many programs that want to run, but each processor core can run only one at a time. Priority scheduling decides who goes next by giving every program a rank. The program with the highest rank always goes first.

It works like a hospital emergency room. A patient with a heart attack is seen before someone with a sprained ankle, regardless of who arrived first. The risk is that the person with the sprain could wait forever if serious cases keep coming, so the system also raises the rank of anyone who has waited too long.

Where it is applied

A team building an industrial controller on embedded Linux runs three threads: a motor control loop every 1 millisecond, a sensor logger every 100 milliseconds and a web dashboard for operators. They assign the control loop the real-time SCHED_FIFO policy at priority 80, the logger priority 40, and leave the dashboard under the normal fair scheduler.

During testing, the control loop occasionally overran its deadline. Tracing showed that it waited on a mutex held by the logger while the dashboard preempted the logger, a textbook priority inversion. Switching the shared mutex to a priority inheritance mutex removed the delays, and worst-case control-loop latency dropped below 100 microseconds under full dashboard load.