Linux Process Scheduling & CPU Affinity: Strategies, Commands, cgroup Guide
This article explains Linux's process scheduling policies—including real‑time, normal, batch, idle, and deadline—how priorities are assigned, the default CFS scheduler, and practical methods to adjust scheduling and CPU affinity using the chrt command, systemd settings, and cgroup cpuset controls.
Scheduling Policies
Linux can run multiple processes on a single CPU by interleaving their execution. The kernel scheduler decides which process runs at any moment, balancing fast decision making, fair CPU time distribution (with higher priority processes receiving more time and preempting lower ones), responsive interactive applications, and predictable scalability under varied workloads.
Process Priorities
Scheduling decisions are based on the policy and priority assigned to each thread or process. Policies are divided into real‑time and non‑real‑time categories.
Available Scheduling Policies (RHEL)
Real‑time
SCHED_FIFO: First‑in‑first‑out without time slicing; a process runs until it blocks on I/O or is preempted by a higher‑priority process.
SCHED_RR: Round‑robin with time slices; processes of equal priority share CPU time in a cyclic order.
Non‑real‑time
SCHED_NORMAL (OTHER): Default policy for most Linux processes.
SCHED_BATCH: Suitable for batch processing workloads.
SCHED_IDLE: Allows low‑priority applications to run when the CPU is idle.
CFS Scheduler
Since kernel 2.6.23, the Completely Fair Scheduler (CFS) is the default. It uses a red‑black tree and virtual time to select the process that has waited the longest, granting it CPU time while its virtual time decreases.
Deadline Scheduler
RHEL 8 introduces SCHED_DEADLINE for real‑time systems, guaranteeing execution based on three parameters: period, deadline, and runtime (worst‑case execution time).
Period: The interval at which a task repeats (e.g., 16 ms for 60 fps video processing).
Deadline: The maximum time by which the task must finish.
Runtime: The maximum CPU time the task may consume.
All time units are in nanoseconds; for example, a task with a 16.6 ms period must receive 5 ms of CPU time, which must be completed within a 10 ms deadline.
Changing Scheduling Options via Command Line
Administrators can use chrt to view or set a process's policy and priority. When launching a new program with chrt, the default policy is SCHED_RR if none is specified.
Policy options:
-b SCHED_BATCH
-f SCHED_FIFO
-i SCHED_IDLE
-o SCHED_NORMAL (OTHER)
-r SCHED_RR
-d SCHED_DEADLINEExample: assigning a policy and priority to a new process.
Changing Scheduling Options via systemd
When defining a service, the [Service] section can specify:
CPUSchedulingPolicy: Sets the service's CPU scheduling policy (other, batch, idle, fifo, rr). Deadline is not currently supported.
CPUSchedulingPriority: Sets the priority (1‑99 for real‑time policies).
After editing, reload systemd with systemctl daemon-reload and restart the service.
CPU Affinity
Pinning processes to specific CPUs can improve cache locality and overall performance. Systemd services can set CPUAffinity in the [Service] section, providing a space‑separated list of CPU indices (e.g., "0 1").
Managing CPU Affinity with cgroup
On NUMA systems, binding a process to CPUs within the same node reduces memory access latency. The cpuset cgroup controller allows specifying which CPUs and memory nodes a group of tasks may use.
Key files in /sys/fs/cgroup/cpuset:
cpuset.cpus – List of CPUs the cgroup can access (e.g., "0-3").
cpuset.mems – List of NUMA memory nodes the cgroup can access.
Use the tuna tool to view CPU bindings:
yum install tuna
使用 tuna 命令查看 CPU 绑定
选顶: -t -P (大写) 查看特定线程的信息,例如调度策略,优先级,CPU 绑定等Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
