Linux Real-Time Schedulers: SCHED_FIFO and SCHED_RR
The Linux kernel implements real-time scheduling policies to provide
deterministic, low-latency execution for mission-critical tasks that
cannot tolerate the unpredictable delays of standard scheduling.
Primarily defined by the POSIX standard, the two core real-time
policies—SCHED_FIFO (First-In, First-Out) and
SCHED_RR (Round-Robin)—grant designated processes strict
priority over standard workloads. This article explains the
architectural role of these policies, how they manage CPU execution,
their core operational differences, and the safety considerations
required when deploying them.
The Role of Real-Time Scheduling in Linux
Standard Linux processes utilize the Completely Fair Scheduler
(SCHED_OTHER or SCHED_NORMAL), which is
designed to maximize overall system throughput and balance CPU time
equitably among all processes. However, multimedia streaming, industrial
automation, robotics, and telecommunications require immediate CPU
access rather than throughput fairness.
To meet these strict deadlines, Linux implements a priority-based preemptive scheduling architecture. Real-time priorities range from 1 (lowest real-time priority) to 99 (highest real-time priority). Any task configured with a real-time policy and a priority between 1 and 99 will instantly preempt any standard process operating at priority 0. A real-time task will hold the CPU until it yields, sleeps, finishes, or is preempted by a task with an even higher real-time priority.
SCHED_FIFO (First In, First Out)
SCHED_FIFO is a static, run-to-completion scheduling
policy for threads of equal priority. It does not use time-slicing.
Execution Model
- Queue Placement: When a
SCHED_FIFOtask becomes runnable, it is placed at the end of the run queue for its specific priority level. - Continuous Execution: Once the task gains control
of the CPU, it continues running until one of the following events
occurs:
- It blocks on an I/O operation or waits for a synchronization primitive (e.g., a mutex).
- It voluntarily yields the processor using the
sched_yield()system call. - It terminates.
- A higher-priority real-time task becomes runnable, preempting it.
- Preemption Recovery: If a higher-priority task
preempts a
SCHED_FIFOtask, the preempted task retains its position at the head of the queue for its priority level and resumes immediately once the higher-priority task stops running.
Primary Use Case
SCHED_FIFO is designed for time-critical, discrete
operations that must complete with minimal context-switching overhead,
such as real-time audio synthesis, hardware controller loops, or direct
device driver interactions.
SCHED_RR (Round-Robin)
SCHED_RR is an extension of SCHED_FIFO
designed to provide fair execution time among multiple real-time tasks
running at the exact same priority level.
Execution Model
- Time Quantum Allocation: Unlike
SCHED_FIFO, tasks assigned toSCHED_RRare allocated a predetermined time slice (quantum). - Round-Robin Rotation: A
SCHED_RRtask runs until its time quantum expires. Once the quantum is exhausted, the scheduler moves the task to the tail of the list for its priority level, allowing the next equal-priority task to run. - Preemption Behavior: Preemption remains strictly
priority-driven. A
SCHED_RRtask of priority 50 will immediately preempt any task (whetherSCHED_FIFOorSCHED_RR) of priority 49 or lower, regardless of remaining time slices.
Primary Use Case
SCHED_RR is suitable when multiple real-time threads
need equal access to CPU resources at the same urgency level, such as
parallel data-processing pipelines handling real-time sensor
streams.
Key Differences
| Feature | SCHED_FIFO | SCHED_RR |
|---|---|---|
| Time Slicing | None | Yes (fixed time quantum) |
| Equal-Priority Handling | Run-to-completion (FIFO order) | Cyclic execution (Round-Robin) |
| Context Switch Overhead | Lower | Slightly higher due to quantum expiration |
| Starvation within Priority Level | Possible if a task does not yield or block | Prevented by time-slice rotation |
Safety and Throttling
Because real-time tasks strictly dominate standard processes, an
infinite loop in a SCHED_FIFO or SCHED_RR task
can starve standard user applications, system daemons, and even the
interactive shell, completely freezing the system.
To mitigate this risk, modern Linux kernels include real-time
bandwidth management via the sysctl parameters:
/proc/sys/kernel/sched_rt_period_us: Defines the measurement period in microseconds (typically 1,000,000 µs / 1 second)./proc/sys/kernel/sched_rt_runtime_us: Defines the maximum total execution time allocated to all real-time tasks within that period (typically 950,000 µs / 0.95 seconds).
This default configuration reserves 5% of CPU time for non-real-time tasks, ensuring administrators retain terminal access to recover or terminate misbehaving real-time processes.