sched(7) Miscellaneous Information Manual sched(7) sched - Linux 2.6.23 CFS -- << >> (Completely Fair Scheduler). CFS <>. , ( , ) Linux : nice(2) . getpriority(2) , , . setpriority(2) , , . sched_setscheduler(2) . sched_getscheduler(2) . sched_setparam(2) . sched_getparam(2) . sched_get_priority_max(2) , . sched_get_priority_min(2) , . sched_rr_get_interval(2) , , <<>> (round-robin) . sched_yield(2) . sched_setaffinity(2) ( Linux) . sched_getaffinity(2) ( Linux) . sched_setattr(2) . ( Linux) sched_setscheduler(2) sched_setparam(2). sched_getattr(2) . ( Linux) sched_setscheduler(2) sched_setparam(2). -- , . , sched_priority. . , (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority ( 0). , (SCHED_FIFO, SCHED_RR), sched_priority 1 () 99 () , . : POSIX.1 32 , . sched_get_priority_min(2) sched_get_priority_max(2) , . , sched_priority. , . , . : , . . SCHED_FIFO: << -- >> 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: o , SCHED_FIFO , , , . o SCHED_FIFO , . o sched_setscheduler(2), sched_setparam(2), sched_setattr(2), pthread_setschedparam(3) pthread_setschedprio(3) SCHED_FIFO, pid, : () , . , . () , . () , . POSIX.1-2008 ( ) - pthread_setschedprio(3), . o , sched_yield(2), . SCHED_FIFO . SCHED_FIFO , /, sched_yield(2). SCHED_RR: SCHED_RR -- SCHED_FIFO. , SCHED_FIFO, SCHED_RR , . SCHED_RR , , . SCHED_RR, , , . , sched_rr_get_interval(2). SCHED_DEADLINE: 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. -- (jobs), . , -- , . , - , (arrival time) ( (request time) (release time). -- . . : / | | | | | v v v -----x--------xooooooooooooooooo--------x--------x--- |<- comp. time ->| |<------- ------>| |<-------------- ------------------->| SCHED_DEADLINE sched_setattr(2) : Runtime, Deadline Period. : , Runtime ( ), Deadline , Period . , SCHED_DEADLINE : / | | | | | v v v -----x--------xooooooooooooooooo--------x--------x--- |<-- Runtime ------->| |<----------- Deadline ----------->| |<-------------- Period ------------------->| sched_runtime, sched_deadline sched_period sched_attr; sched_setattr(2). . sched_period 0, sched_deadline. : 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. CBS , (throttling) , (Runtime). , , SCHED_DEADLINE ( ) . SCHED_DEADLINE . , ; , sched_setattr(2) EBUSY. , ( ), , , Runtime, Period. , , SCHED_DEADLINE, SCHED_DEADLINE ( ) ; SCHED_DEADLINE , , . fork(2) , SCHED_DEADLINE EAGAIN, reset-on-fork ( ). SCHED_DEADLINE, sched_yield(2) . SCHED_OTHER: ( Linux) SCHED_OTHER 0 ( , , , SCHED_OTHER). SCHED_OTHER -- Linux , , . 0 , . ( ) , , . SCHED_OTHER. Linux SCHED_OTHER SCHED_NORMAL. -- , ( ) . SCHED_OTHER SCHED_BATCH ( ). nice(2), setpriority(2) sched_setattr(2). POSIX.1, ; . Linux : . 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. SCHED_OTHER UNIX Linux. 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). , , Linux RLIMIT_NICE; setrlimit(2). . SCHED_BATCH: ( Linux 2.6.16) SCHED_BATCH . SCHED_OTHER , ( nice). , , , , . , , . , nice , , ( ). SCHED_IDLE: ( Linux 2.6.23) SCHED_IDLE ; nice . ( nice +19 SCHED_OTHER SCHED_BATCH). reset-on-fork. , , fork(2), . reset-on-fork : o SCHED_RESET_ON_FORK policy sched_setscheduler(2) ( Linux 2.6.32); o SCHED_FLAG_RESET_ON_FORK attr.sched_flags sched_setattr(2). , , . reset-on-fork sched_getscheduler(2) sched_getattr(2). reset-on-fork , -, RLIMIT_RTTIME (. getrlimit(2)), . , reset-on-fork, : o SCHED_FIFO SCHED_RR, SCHED_OTHER. o nice , nice . reset-on-fork , CAP_SYS_NICE. , fork(2). 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. SCHED_DEADLINE (CAP_SYS_NICE). Linux 2.6.12, RLIMIT_RTPRIO SCHED_RR SCHED_FIFO. : o RLIMIT_RTPRIO, , RLIMIT_RTPRIO. o RLIMIT_RTPRIO 0, . o , , , . o 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)). (CAP_SYS_NICE) RLIMIT_RTPRIO ; , . getrlimit(2) RLIMIT_RTPRIO. 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. Linux 2.6.25, . -- RLIMIT_RTTIME, , . getrlimit(2). 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 , 100% . 1 INT_MAX, 1 , , 35 . 1000000 (1 ). /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. , /, , . , <<>> . fork(2). execve(2). , , . mlock(2) mlockall(2). Linux 2.6.38 , ; , Linux (. ., make(1) -j). CFS, CONFIG_SCHED_AUTOGROUP. /proc/sys/kernel/sched_autogroup_enabled; 0 , 1 -- . 1, noautogroup. setsid(2); , . , fork(2), . , . . << >> . CFS , . , . Suppose that there are two autogroups competing for the same CPU (i.e., presume either a single CPU system or the use of taskset(1) to confine all the processes to the same CPU on an SMP system). The first group contains ten CPU-bound processes from a kernel build started with make -j10. The other contains a single CPU-bound process: a video player. The effect of autogrouping is that the two groups will each receive half of the CPU cycles. That is, the video player will receive 50% of the CPU cycles, rather than just 9% of the cycles, which would likely lead to degraded video playback. The situation on an SMP system is more complex, but the general effect is the same: the scheduler distributes CPU cycles across task groups such that an autogroup that contains a large number of CPU-bound processes does not end up hogging CPU cycles at the expense of the other jobs on the system. A process's autogroup (task group) membership can be viewed via the file /proc/pid/autogroup: $ cat /proc/1/autogroup /autogroup-1 nice 0 , . <<>>, . : +19 ( ) -20 ( ) ( write(2) EINVAL). , , . , , ( ) ( ). cgroups(7) cgroup cgroup , . , (SCHED_OTHER, SCHED_BATCH SCHED_IDLE). , (deadline). , . (. ., SCHED_OTHER, SCHED_BATCH SCHED_IDLE), CFS << >>, CONFIG_FAIR_GROUP_SCHED ( ). << >>. , , << >>. : o cgroup . cgroup. o , , () (. ., , setsid(2)), . , . . o , cgroup , . o , cgroup . o (. ., CONFIG_FAIR_GROUP_SCHED), . . UNIX. , ( ), setpriority(2) nice(1) , (, ). , , () (, , ), . , , : $ echo 10 > /proc/self/autogroup Linux Since Linux 2.6.18, Linux is gradually becoming equipped with real-time capabilities, most of which are derived from the former realtime-preempt patch set. Until the patches have been completely merged into the mainline kernel, they must be installed to achieve the best real-time performance. These patches are named: patch-_-rt_ . , : CONFIG_PREEMPT_NONE, CONFIG_PREEMPT_VOLUNTARY CONFIG_PREEMPT_DESKTOP, , , , . , CONFIG_PREEMPT_RT. , Linux . FIFO RR. cgroups(7). Originally, Standard Linux was intended as a general-purpose operating system being able to handle background processes, interactive applications, and less demanding real-time applications (applications that need to usually meet timing deadlines). Although the Linux 2.6 allowed for kernel preemption and the newly introduced O(1) scheduler ensures that the time needed to schedule is fixed and deterministic irrespective of the number of active tasks, true real-time computing was not possible up to Linux 2.6.17. . 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) Programming for the real world - POSIX.4 by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0. The Linux kernel source files Documentation/scheduler/sched-deadline .txt, Documentation/scheduler/sched-rt-group.txt, Documentation/ scheduler/sched-design-CFS.txt, and Documentation/scheduler/ sched-nice-design.txt Alexander Golubev , Azamat Hackimov , Hotellook, Nikita , Spiros Georgaras , Vladislav , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . sched(7)