sched(7)

โœจ NAME โœจ

sched โ€” overview of CPU scheduling ๐ŸŽ€ (this is the sched(7) page from the Miscellaneous Information Manual, aka section 7, and it's giving CPU scheduling overview, periodt ๐Ÿ’…)

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so ever since Linux 2.6.23, the default scheduler is CFS, the "Completely Fair Scheduler" โœจ. CFS replaced the earlier "O(1)" scheduler. iconic behavior, honestly ๐Ÿ‘‘.

๐ŸŒธ API summary ๐ŸŒธ

Linux gives you these system calls for controlling the CPU scheduling behavior, policy, and priority of processes (or, more precisely, threads) ๐Ÿซถ:

  • nice(2) โ€” sets a new nice value for the calling thread and returns the new nice value ๐Ÿ’–
  • getpriority(2) โ€” returns the nice value of a thread, a process group, or the set of threads owned by a specified user โœจ
  • setpriority(2) โ€” sets the nice value of a thread, a process group, or the set of threads owned by a specified user ๐ŸŽ€
  • sched_setscheduler(2) โ€” sets the scheduling policy and parameters of a specified thread ๐Ÿ’…
  • sched_getscheduler(2) โ€” returns the scheduling policy of a specified thread ๐ŸŒธ
  • sched_setparam(2) โ€” sets the scheduling parameters of a specified thread ๐Ÿฆ‹
  • sched_getparam(2) โ€” fetches the scheduling parameters of a specified thread ๐Ÿ’œ
  • sched_get_priority_max(2) โ€” returns the maximum priority available in a specified scheduling policy ๐Ÿ”ฅ
  • sched_get_priority_min(2) โ€” returns the minimum priority available in a specified scheduling policy ๐Ÿ”ฅ
  • sched_rr_get_interval(2) โ€” fetches the quantum used for threads scheduled under the "round-robin" scheduling policy โณ
  • sched_yield(2) โ€” causes the caller to relinquish the CPU, so that some other thread be executed ๐Ÿ™ˆ
  • sched_setaffinity(2) โ€” (Linux-specific) sets the CPU affinity of a specified thread ๐ŸŽฏ
  • sched_getaffinity(2) โ€” (Linux-specific) gets the CPU affinity of a specified thread ๐ŸŽฏ
  • sched_setattr(2) โ€” sets the scheduling policy and parameters of a specified thread. This (Linux-specific) system call provides a superset of the functionality of sched_setscheduler(2) and sched_setparam(2) ๐Ÿ’…
  • sched_getattr(2) โ€” fetches the scheduling policy and parameters of a specified thread. This (Linux-specific) system call provides a superset of the functionality of sched_getscheduler(2) and sched_getparam(2) ๐Ÿ’…

๐ŸŽ€ Scheduling policies ๐ŸŽ€

The scheduler is the kernel component that decides which runnable thread will be executed by the CPU next โœจ. Every thread has an associated scheduling policy and a static scheduling priority, sched_priority. The scheduler makes its decisions based on knowledge of the scheduling policy and static priority of all threads on the system ๐Ÿง .

For threads scheduled under one of the normal scheduling policies (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority is not used in scheduling decisions (it must be specified as 0) ๐Ÿšซ.

Processes scheduled under one of the real-time policies (SCHED_FIFO, SCHED_RR) have a sched_priority value in the range 1 (low) to 99 (high) ๐Ÿ”ฅ. (As the numbers imply, real-time threads always have higher priority than normal threads.) Note well: POSIX.1 requires an implementation to support only a minimum 32 distinct priority levels for the real-time policies, and some systems supply just this minimum ๐Ÿ˜ญ. Portable programs should use sched_get_priority_min(2) and sched_get_priority_max(2) to find the range of priorities supported for a particular policy ๐Ÿซถ.

Conceptually, the scheduler maintains a list of runnable threads for each possible sched_priority value ๐Ÿ“‹. In order to determine which thread runs next, the scheduler looks for the nonempty list with the highest static priority and selects the thread at the head of this list ๐Ÿ‘‘.

A thread's scheduling policy determines where it will be inserted into the list of threads with equal static priority and how it will move inside this list ๐Ÿ’….

All scheduling is preemptive: if a thread with a higher static priority becomes ready to run, the currently running thread will be preempted and returned to the wait list for its static priority level โšก. The scheduling policy determines the ordering only within the list of runnable threads with equal static priority ๐ŸŽ€.

๐Ÿ”ฅ SCHED_FIFO: First in-first out scheduling ๐Ÿ”ฅ

SCHED_FIFO can be used only with static priorities higher than 0, which means that when a SCHED_FIFO thread becomes runnable, it will always immediately preempt any currently running SCHED_OTHER, SCHED_BATCH, or SCHED_IDLE thread ๐Ÿ‘‘. SCHED_FIFO is a simple scheduling algorithm without time slicing โœจ. For threads scheduled under the SCHED_FIFO policy, the following rules apply:

  • A running SCHED_FIFO thread that has been preempted by another thread of higher priority will stay at the head of the list for its priority and will resume execution as soon as all threads of higher priority are blocked again ๐Ÿ’….
  • When a blocked SCHED_FIFO thread becomes runnable, it will be inserted at the end of the list for its priority ๐Ÿฆ‹.
  • If a call to sched_setscheduler(2), sched_setparam(2), sched_setattr(2), pthread_setschedparam(3), or pthread_setschedprio(3) changes the priority of the running or runnable SCHED_FIFO thread identified by pid the effect on the thread's position in the list depends on the direction of the change to the thread's priority:
    • (a) If the thread's priority is raised, it is placed at the end of the list for its new priority. As a consequence, it may preempt a currently running thread with the same priority โฌ†๏ธ.
    • (b) If the thread's priority is unchanged, its position in the run list is unchanged โžก๏ธ.
    • (c) If the thread's priority is lowered, it is placed at the front of the list for its new priority โฌ‡๏ธ.

According to POSIX.1-2008, changes to a thread's priority (or policy) using any mechanism other than pthread_setschedprio(3) should result in the thread being placed at the end of the list for its priority ๐Ÿ“œ.

  • A thread calling sched_yield(2) will be put at the end of the list ๐Ÿ™ˆ.

No other events will move a thread scheduled under the SCHED_FIFO policy in the wait list of runnable threads with equal static priority ๐Ÿšซ.

A SCHED_FIFO thread runs until either it is blocked by an I/O request, it is preempted by a higher priority thread, or it calls sched_yield(2) ๐Ÿ’€.

๐ŸŒˆ SCHED_RR: Round-robin scheduling ๐ŸŒˆ

SCHED_RR is a simple enhancement of SCHED_FIFO โœจ. Everything described above for SCHED_FIFO also applies to SCHED_RR, except that each thread is allowed to run only for a maximum time quantum โณ. If a SCHED_RR thread has been running for a time period equal to or longer than the time quantum, it will be put at the end of the list for its priority ๐Ÿ”„. A SCHED_RR thread that has been preempted by a higher priority thread and subsequently resumes execution as a running thread will complete the unexpired portion of its round-robin time quantum ๐ŸŽ€. The length of the time quantum can be retrieved using sched_rr_get_interval(2) ๐Ÿซถ.

๐Ÿฆ‹ SCHED_DEADLINE: Sporadic task model deadline scheduling ๐Ÿฆ‹

Since Linux 3.14, Linux provides a deadline scheduling policy (SCHED_DEADLINE) โฐ. This policy is currently implemented using GEDF (Global Earliest Deadline First) in conjunction with CBS (Constant Bandwidth Server) ๐Ÿ”ฅ. To set and fetch this policy and associated attributes, one must use the Linux-specific sched_setattr(2) and sched_getattr(2) system calls ๐Ÿ’….

A sporadic task is one that has a sequence of jobs, where each job is activated at most once per period ๐Ÿ“…. Each job also has a relative deadline, before which it should finish execution, and a computation time, which is the CPU time necessary for executing the job โฑ๏ธ. The moment when a task wakes up because a new job has to be executed is called the arrival time (also referred to as the request time or release time) ๐Ÿš€. The start time is the time at which a task starts its execution ๐Ÿ. The absolute deadline is thus obtained by adding the relative deadline to the arrival time ๐ŸŽฏ.

The following diagram clarifies these terms:

           arrival/wakeup                    absolute deadline
                |    start time                    |
                |        |                         |
                v        v                         v
           -----x--------xooooooooooooooooo--------x--------x---
                         |<- comp. time ->|
                |<------- relative deadline ------>|
                |<-------------- period ------------------->|

When setting a SCHED_DEADLINE policy for a thread using sched_setattr(2), one can specify three parameters: Runtime, Deadline, and Period ๐Ÿ“. These parameters do not necessarily correspond to the aforementioned terms: usual practice is to set Runtime to something bigger than the average computation time (or worst-case execution time for hard real-time tasks), Deadline to the relative deadline, and Period to the period of the task ๐Ÿง . Thus, for SCHED_DEADLINE scheduling, we have:

           arrival/wakeup                    absolute deadline
                |    start time                    |
                |        |                         |
                v        v                         v
           -----x--------xooooooooooooooooo--------x--------x---
                         |<-- Runtime ------->|
                |<----------- Deadline ----------->|
                |<-------------- Period ------------------->|

The three deadline-scheduling parameters correspond to the sched_runtime, sched_deadline, and sched_period fields of the sched_attr structure; see sched_setattr(2) ๐Ÿ“š. These fields express values in nanoseconds โฑ๏ธ. If sched_period is specified as 0, then it is made the same as sched_deadline ๐ŸŽ€.

The kernel requires that:

           sched_runtime <= sched_deadline <= sched_period

In addition, under the current implementation, all of the parameter values must be at least 1024 (i.e., just over one microsecond, which is the resolution of the implementation), and less than 2^63 ๐Ÿ”ข. If any of these checks fails, sched_setattr(2) fails with the error EINVAL ๐Ÿ’€.

The CBS guarantees non-interference between tasks, by throttling threads that attempt to over-run their specified Runtime ๐Ÿ›‘.

To ensure deadline scheduling guarantees, the kernel must prevent situations where the set of SCHED_DEADLINE threads is not feasible (schedulable) within the given constraints ๐Ÿงฉ. The kernel thus performs an admittance test when setting or changing SCHED_DEADLINE policy and attributes ๐Ÿ”. This admission test calculates whether the change is feasible; if it is not, sched_setattr(2) fails with the error EBUSY ๐Ÿ˜ญ.

For example, it is required (but not necessarily sufficient) for the total utilization to be less than or equal to the total number of CPUs available, where, since each thread can maximally run for Runtime per Period, that thread's utilization is its Runtime divided by its Period ๐Ÿ“Š.

And to actually make good on all those promises it makes the second a thread gets admitted to SCHED_DEADLINE: those threads are the highest priority (user controllable) threads in the entire system. If any SCHED_DEADLINE thread is runnable, it will preempt any thread scheduled under one of the other policies. no line, no waiting. ๐Ÿ’…

Also, a call to fork(2) by a thread scheduled under the SCHED_DEADLINE policy fails with the error EAGAIN โ€” unless the thread has its reset-on-fork flag set (peep below, bestie, I got you ๐Ÿซถ).

And a SCHED_DEADLINE thread that calls sched_yield(2) will yield the current job and wait for a new period to begin.

โœจ SCHED_OTHER: Default Linux time-sharing scheduling โœจ

SCHED_OTHER can be used at only static priority 0 (i.e., threads under real-time policies always have priority over SCHED_OTHER processes). It's the standard Linux time-sharing scheduler, the one that's intended for all the threads that do not require the special real-time mechanisms.

The thread to run gets picked out of the static priority 0 list based on a dynamic priority that is determined only inside this list. That dynamic priority is based on the nice value (see below) and it goes up for each time quantum the thread is ready to run, but denied to run by the scheduler. This ensures fair progress among all SCHED_OTHER threads. ๐Ÿซถ

Fun lil fact: in the Linux kernel source code, the SCHED_OTHER policy is actually named SCHED_NORMAL. iykyk. ๐Ÿฆ‹

๐ŸŽ€ The nice value ๐ŸŽ€

The nice value is an attribute that can be used to influence the CPU scheduler to favor or disfavor a process in scheduling decisions. It affects the scheduling of SCHED_OTHER and SCHED_BATCH (see below) processes. The nice value can be modified using nice(2), setpriority(2), or sched_setattr(2).

According to POSIX.1, the nice value is a per-process attribute; that is, the threads in a process should share a nice value. However, on Linux, the nice value is a per-thread attribute: different threads in the same process may have different nice values. not the plot twist POSIX wanted, but here we are. ๐Ÿ’€

The range of the nice value varies across UNIX systems. On modern Linux, the range is -20 (high priority) to +19 (low priority). On some other systems, the range is -20..20. Very early Linux kernels (before Linux 2.0) had the range -infinity..15. ๐Ÿ’€ iconic chaotic era tbh

The degree to which the nice value affects the relative scheduling of SCHED_OTHER processes likewise varies across UNIX systems and across Linux kernel versions.

With the advent of the CFS scheduler in Linux 2.6.23, Linux adopted an algorithm that causes relative differences in nice values to have a much stronger effect. In the current implementation, each unit of difference in the nice values of two processes results in a factor of 1.25 in the degree to which the scheduler favors the higher priority process. This causes very low nice values (+19) to truly provide little CPU to a process whenever there is any other higher priority load on the system, and makes high nice values (-20) deliver most of the CPU to applications that require it (e.g., some audio applications). ๐ŸŽง

On Linux, the RLIMIT_NICE resource limit can be used to define a limit to which an unprivileged process's nice value can be raised; see setrlimit(2) for details.

For further details on the nice value, see the subsections on the autogroup feature and group scheduling, below.

๐ŸŒธ SCHED_BATCH: Scheduling batch processes ๐ŸŒธ

(Since Linux 2.6.16.) SCHED_BATCH can be used only at static priority 0. This policy is similar to SCHED_OTHER in that it schedules the thread according to its dynamic priority (based on the nice value). The difference is that this policy will cause the scheduler to always assume that the thread is CPU-intensive. Consequently, the scheduler will apply a small scheduling penalty with respect to wakeup behavior, so that this thread is mildly disfavored in scheduling decisions.

This policy is useful for workloads that are noninteractive, but do not want to lower their nice value, and for workloads that want a deterministic scheduling policy without interactivity causing extra preemptions (between the workload's tasks). periodt. ๐Ÿ’…

๐Ÿฆ‹ SCHED_IDLE: Scheduling very low priority jobs ๐Ÿฆ‹

(Since Linux 2.6.23.) SCHED_IDLE can be used only at static priority 0; the process nice value has no influence for this policy.

This policy is intended for running jobs at extremely low priority (lower even than a +19 nice value with the SCHED_OTHER or SCHED_BATCH policies). it's giving background noise, respectfully. ๐Ÿ™ˆ

๐Ÿ“ Resetting scheduling policy for child processes ๐Ÿ“

Each thread has a reset-on-fork scheduling flag. When this flag is set, children created by fork(2) do not inherit privileged scheduling policies. The reset-on-fork flag can be set by either:

  • ORing the SCHED_RESET_ON_FORK flag into the policy argument when calling sched_setscheduler(2) (since Linux 2.6.32); or
  • specifying the SCHED_FLAG_RESET_ON_FORK flag in attr.sched_flags when calling sched_setattr(2).
not me pointing out that the constants used with these two APIs have different names. stay woke. ๐Ÿ‘€

The state of the reset-on-fork flag can analogously be retrieved using sched_getscheduler(2) and sched_getattr(2).

The reset-on-fork feature is intended for media-playback applications, and can be used to prevent applications evading the RLIMIT_RTTIME resource limit (see getrlimit(2)) by creating multiple child processes.

More precisely, if the reset-on-fork flag is set, the following rules apply for subsequently created children:

  • If the calling thread has a scheduling policy of SCHED_FIFO or SCHED_RR, the policy is reset to SCHED_OTHER in child processes.
  • If the calling process has a negative nice value, the nice value is reset to zero in child processes.

After the reset-on-fork flag has been enabled, it can be reset only if the thread has the CAP_SYS_NICE capability. This flag is disabled in child processes created by fork(2).

๐Ÿ’œ Privileges and resource limits ๐Ÿ’œ

Before Linux 2.6.12, only privileged (CAP_SYS_NICE) threads can set a nonzero static priority (i.e., set a real-time scheduling policy). The only change that an unprivileged thread can make is to set the SCHED_OTHER policy, and this can be done only if the effective user ID of the caller matches the real or effective user ID of the target thread (i.e., the thread specified by pid) whose policy is being changed.

A thread must be privileged (CAP_SYS_NICE) in order to set or modify a SCHED_DEADLINE policy.

Since Linux 2.6.12, the RLIMIT_RTPRIO resource limit defines a ceiling on an unprivileged thread's static priority for the SCHED_RR and SCHED_FIFO policies. The rules for changing scheduling policy and priority are as follows:

  • If an unprivileged thread has a nonzero RLIMIT_RTPRIO soft limit, then it can change its scheduling policy and priority, subject to the restriction that the priority cannot be set to a value higher than the maximum of its current priority and its RLIMIT_RTPRIO soft limit.
  • If the RLIMIT_RTPRIO soft limit is 0, then the only permitted changes are to lower the priority, or to switch to a non-real-time policy.
  • Subject to the same rules, another unprivileged thread can also make these changes, as long as the effective user ID of the thread making the change matches the real or effective user ID of the target thread.
  • Special rules apply for the SCHED_IDLE policy. Before Linux 2.6.39, an unprivileged thread operating under this policy cannot change its policy, regardless of the value of its RLIMIT_RTPRIO resource limit. Since Linux 2.6.39, an unprivileged thread can switch to either the SCHED_BATCH or the SCHED_OTHER policy so long as its nice value falls within the range permitted by its RLIMIT_NICE resource limit (see getrlimit(2)).

Privileged (CAP_SYS_NICE) threads ignore the RLIMIT_RTPRIO limit; as with older kernels, they can make arbitrary changes to scheduling policy and priority. See getrlimit(2) for further information on RLIMIT_RTPRIO.

๐Ÿ”ฅ Limiting the CPU usage of real-time and deadline processes ๐Ÿ”ฅ

A nonblocking infinite loop in a thread scheduled under the SCHED_FIFO, SCHED_RR, or SCHED_DEADLINE policy can potentially block all other threads from accessing the CPU forever. Before Linux 2.6.25, the only way of preventing a runaway real-time process from freezing the system was to run (at the console) a shell scheduled under a higher static priority than the tested application. This allows an emergency kill of tested real-time applications that do not block or terminate as expected.

Since Linux 2.6.25, there are other techniques for dealing with runaway real-time and deadline processes. One of these is to use the RLIMIT_RTTIME resource limit to set a ceiling on the CPU time that a real-time process may consume. See getrlimit(2) for details.

Since Linux 2.6.25, Linux also provides two /proc files that can be used to reserve a certain amount of CPU time to be used by non-real-time processes. Reserving CPU time in this fashion allows some CPU time to be allocated to (say) a root shell that can be used to kill a runaway process. Both of these files specify time values in microseconds:

  • /proc/sys/kernel/sched_rt_period_us โ€” This file specifies a scheduling period that is equivalent to 100% CPU bandwidth. The value in this file can range from 1 to INT_MAX, giving an operating range of 1 microsecond to around 35 minutes. The default value in this file is 1,000,000 (1 second).
  • /proc/sys/kernel/sched_rt_runtime_us โ€” The value in this file specifies how much of the "period" time can be used by all real-time and deadline scheduled processes on the system. The value in this file can range from -1 to INT_MAX-1. Specifying -1 makes the run time the same as the period; that is, no CPU time is set aside for non-real-time processes (which was the behavior before Linux 2.6.25). The default value in this file is 950,000 (0.95 seconds), meaning that 5% of the CPU time is reserved for processes that don't run under a real-time or deadline scheduling policy.

โญ Response time โญ

A blocked high priority thread waiting for I/O has a certain response time before it is scheduled again. The device driver writer can greatly reduce this response time by using a "slow interrupt" interrupt handler.

๐ŸŒˆ Miscellaneous ๐ŸŒˆ

Child processes inherit the scheduling policy and parameters across a fork(2). The scheduling policy and parameters are preserved across execve(2).

Memory locking is usually needed for real-time processes to avoid paging delays; this can be done with mlock(2) or mlockall(2). ๐Ÿซถ

๐ŸŒธ The autogroup feature ๐ŸŒธ

okay so picture this: since Linux 2.6.38 the kernel ships with a lil feature called autogrouping, and its whole entire job is making your interactive desktop feel snappy even when something is absolutely devouring your CPU โ€” think building the Linux kernel with a ton of parallel build processes (yes bestie, that's the make(1) -j flag ๐Ÿซ ).

This feature works together with the CFS scheduler and it needs a kernel that was configured with CONFIG_SCHED_AUTOGROUP. On a live running system you flip it on or off through the file /proc/sys/kernel/sched_autogroup_enabled โ€” a value of 0 disables it, a value of 1 enables it. The default value in that file is 1, unless the kernel got booted with the noautogroup parameter. ๐ŸŽ€

A brand new autogroup gets created whenever a new session is created via setsid(2) โ€” that happens, for example, when you open a new terminal window. And a new process created by fork(2) inherits its parent's autogroup membership. So basically every process in a session is a member of the same autogroup, one cute lil family ๐Ÿซถ. An autogroup is automatically destroyed the moment the last process in the group terminates. periodt.

When autogrouping is enabled, all the members of an autogroup get dropped into the same kernel scheduler "task group". When it's disabled, the group creation still happens exactly as described above and autogroup membership is still visible in /proc, but the autogroups just aren't used. The CFS scheduler runs an algorithm that equalizes how CPU cycles are distributed across task groups, and the desktop-performance glow-up goes like this:

Suppose two autogroups are competing for the same CPU (so: either a single-CPU system, or you used taskset(1) to confine all the processes to one CPU on an SMP system). The first group has ten CPU-bound processes from a kernel build started with make -j10. The other group has exactly one CPU-bound process: a video player. What autogrouping does is give each group half of the CPU cycles. So the video player pulls in 50% of the CPU cycles instead of just 9% โ€” and 9% would very likely mean degraded video playback ๐Ÿ˜ญ. On an SMP system it's messier, but the general effect is the same: the scheduler distributes CPU cycles across task groups so that an autogroup stuffed with CPU-bound processes doesn't end up hogging CPU cycles at the expense of the other jobs on the system.

You can peek at a process's autogroup (task group) membership through the file /proc/pid/autogroup:

$ cat /proc/1/autogroup
/autogroup-1 nice 0

This file can also be used to modify the CPU bandwidth allocated to an autogroup. How? You write a number in the "nice" range into the file, and that sets the autogroup's nice value. The allowed range is from +19 (low priority) to -20 (high priority). (Writing values outside of this range causes write(2) to fail with the error EINVAL โ€” ๐Ÿ’€ don't.)

The autogroup nice setting means the same thing as the process nice value, but it applies to the distribution of CPU cycles to the autogroup as a whole, based on the relative nice values of the other autogroups. And for a process inside an autogroup, the CPU cycles it receives are a product of the autogroup's nice value (compared to other autogroups) and the process's nice value (compared to other processes in the same autogroup).

Heads up bestie ๐Ÿ™ˆ: using the cgroups(7) CPU controller to place processes in cgroups other than the root CPU cgroup overrides the effect of autogrouping.

Also important: the autogroup feature only groups processes scheduled under non-real-time policies (SCHED_OTHER, SCHED_BATCH, and SCHED_IDLE). It does not group processes scheduled under real-time and deadline policies. Those ones get scheduled according to the rules described earlier. ๐Ÿ’…


๐Ÿฆ‹ The nice value and group scheduling ๐Ÿฆ‹

When the scheduler is dealing with non-real-time processes (i.e., the ones under the SCHED_OTHER, SCHED_BATCH, and SCHED_IDLE policies), the CFS scheduler uses a technique called "group scheduling" โ€” assuming the kernel was configured with the CONFIG_FAIR_GROUP_SCHED option, which is typical.

Under group scheduling, threads are scheduled in "task groups". These task groups have a hierarchical relationship, all rooted under the initial task group on the system, which is known as the "root task group". Task groups get formed in these circumstances:

  • All of the threads in a CPU cgroup form a task group. The parent of this task group is the task group of the corresponding parent cgroup.
  • If autogrouping is enabled, then all of the threads that are (implicitly) placed in an autogroup (i.e., the same session, as created by setsid(2)) form a task group. Each new autogroup is thus a separate task group, and the root task group is the parent of all such autogroups.
  • If autogrouping is enabled, then the root task group consists of all processes in the root CPU cgroup that were not otherwise implicitly placed into a new autogroup.
  • If autogrouping is disabled, then the root task group consists of all processes in the root CPU cgroup.
  • If group scheduling was disabled (i.e., the kernel was configured without CONFIG_FAIR_GROUP_SCHED), then all of the processes on the system are notionally placed in a single task group.

Under group scheduling, a thread's nice value only has an effect on scheduling decisions relative to other threads in the same task group. And this has some genuinely surprising consequences for the traditional semantics of the nice value on UNIX systems. In particular, if autogrouping is enabled (which is the default in various distributions), then using setpriority(2) or nice(1) on a process only has an effect for scheduling relative to other processes executed in the same session โ€” typically, the same terminal window. ๐Ÿ˜ณ

Conversely, for two processes that are (for example) the sole CPU-bound processes in different sessions (different terminal windows, each of whose jobs are tied to different autogroups), modifying the nice value of the process in one of the sessions has no effect in terms of the scheduler's decisions relative to the process in the other session. A possibly useful workaround here is a command like this one, which modifies the autogroup nice value for all of the processes in a terminal session:

$ echo 10 > /proc/self/autogroup

โšก Real-time features in the mainline Linux kernel โšก

Since Linux 2.6.18, Linux has been gradually getting equipped with real-time capabilities, most of which come from the former realtime-preempt patch set. These patches are named:

patch-kernelversion-rtpatchversion

and you can download them from http://www.kernel.org/pub/linux/kernel/projects/rt/ or clone them from a git tree at https://git.kernel.org/pub/scm/linux/kernel/git/rt/linux-stable-rt.git. The individual releases are maintained for as long as the matching LTS kernel is maintained. ๐Ÿงš

Since Linux 6.12 it's possible to enable the real-time preemption without needing any patches at all. The realtime-patch does continue to exist though, and it carries support for not-yet-integrated architectures, drivers, and features that are in development.

Since Linux 6.13, real-time became an option and not a preemption model. With that change, these preemption models are available: CONFIG_PREEMPT_NONE, CONFIG_PREEMPT_VOLUNTARY, CONFIG_PREEMPT, and CONFIG_PREEMPT_LAZY. The real-time capabilities can be enabled with the option CONFIG_PREEMPT_RT, and the preemption model can be set to either CONFIG_PREEMPT or CONFIG_PREEMPT_LAZY. That latter model is less eager to preempt SCHED_NORMAL tasks, as an attempt to reduce lock-holder preemption. It does not affect real-time tasks. ๐Ÿ‘‘

With CONFIG_PREEMPT_RT enabled, Linux gets transformed into a regular real-time operating system. The SCHED_FIFO, SCHED_RR, and SCHED_DEADLINE scheduling policies are then used to run a thread with true real-time priority and a minimum worst-case scheduling latency. slay.


๐Ÿ“ NOTES ๐Ÿ“

  • The cgroups(7) CPU controller can be used to limit the CPU consumption of groups of processes.
  • Originally, Standard Linux was intended as a general-purpose operating system able to handle background processes, interactive applications, and less demanding real-time applications (apps that need to usually meet timing deadlines). And even though Linux 2.6 allowed for kernel preemption, and the newly introduced O(1) scheduler ensures the time needed to schedule is fixed and deterministic regardless of the number of active tasks, true real-time computing was not possible up to Linux 2.6.17. ๐Ÿ˜ญ

๐Ÿ’– SEE ALSO ๐Ÿ’–

chcpu(1), chrt(1), lscpu(1), ps(1), taskset(1), top(1), getpriority(2), mlock(2), mlockall(2), munlock(2), munlockall(2), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getaffinity(2), sched_getparam(2), sched_getscheduler(2), sched_rr_get_interval(2), sched_setaffinity(2), sched_setparam(2), sched_setscheduler(2), sched_yield(2), setpriority(2), pthread_getaffinity_np(3), pthread_getschedparam(3), pthread_setaffinity_np(3), sched_getcpu(3), capabilities(7), cpuset(7)


aaaand that's a wrap on the whole entire page bestie ๐ŸŽ€โœจ this one came from Linux man-pages 6.20, dated 2026-02-08, and yes it's sched(7) โ€” go forth and schedule responsibly, ily ๐Ÿ’–

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: GPL-2.0-or-later
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms