1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2014 Peter Zijlstra <peterz@infradead.org>
3 .\" and Copyright (C) 2014 Juri Lelli <juri.lelli@gmail.com>
4 .\" Various pieces from the old sched_setscheduler(2) page
5 .\" Copyright (C) Tom Bjorkholm, Markus Kuhn & David A. Wheeler 1996-1999
6 .\" and Copyright (C) 2007 Carsten Emde <Carsten.Emde@osadl.org>
7 .\" and Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
9 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
10 .\" This is free documentation; you can redistribute it and/or
11 .\" modify it under the terms of the GNU General Public License as
12 .\" published by the Free Software Foundation; either version 2 of
13 .\" the License, or (at your option) any later version.
15 .\" The GNU General Public License's references to "object code"
16 .\" and "executables" are to be interpreted as the output of any
17 .\" document formatting or typesetting system, including
18 .\" intermediate and printed output.
20 .\" This manual is distributed in the hope that it will be useful,
21 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
22 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 .\" GNU General Public License for more details.
25 .\" You should have received a copy of the GNU General Public
26 .\" License along with this manual; if not, see
27 .\" <http://www.gnu.org/licenses/>.
30 .\" Worth looking at: http://rt.wiki.kernel.org/index.php
32 .TH SCHED 7 2016-10-08 "Linux" "Linux Programmer's Manual"
34 sched \- overview of CPU scheduling
36 Since Linux 2.6.23, the default scheduler is CFS,
37 the "Completely Fair Scheduler".
38 The CFS scheduler replaced the earlier "O(1)" scheduler.
41 Linux provides the following system calls for controlling
42 the CPU scheduling behavior, policy, and priority of processes
43 (or, more precisely, threads).
45 .BR sched_setscheduler (2)
46 Set the scheduling policy and parameters of a specified thread.
48 .BR sched_getscheduler (2)
49 Return the scheduling policy of a specified thread.
51 .BR sched_setparam (2)
52 Set the scheduling parameters of a specified thread.
54 .BR sched_getparam (2)
55 Fetch the scheduling parameters of a specified thread.
57 .BR sched_get_priority_max (2)
58 Return the maximum priority available in a specified scheduling policy.
60 .BR sched_get_priority_min (2)
61 Return the minimum priority available in a specified scheduling policy.
63 .BR sched_rr_get_interval (2)
64 Fetch the quantum used for threads that are scheduled under
65 the "round-robin" scheduling policy.
68 Cause the caller to relinquish the CPU,
69 so that some other thread be executed.
71 .BR sched_setaffinity (2)
73 Set the CPU affinity of a specified thread.
75 .BR sched_getaffinity (2)
77 Get the CPU affinity of a specified thread.
80 Set the scheduling policy and parameters of a specified thread.
81 This (Linux-specific) system call provides a superset of the functionality of
82 .BR sched_setscheduler (2)
84 .BR sched_setparam (2).
87 Fetch the scheduling policy and parameters of a specified thread.
88 This (Linux-specific) system call provides a superset of the functionality of
89 .BR sched_getscheduler (2)
91 .BR sched_getparam (2).
93 .SS Scheduling policies
94 The scheduler is the kernel component that decides which runnable thread
95 will be executed by the CPU next.
96 Each thread has an associated scheduling policy and a \fIstatic\fP
99 The scheduler makes its decisions based on knowledge of the scheduling
100 policy and static priority of all threads on the system.
102 For threads scheduled under one of the normal scheduling policies
103 (\fBSCHED_OTHER\fP, \fBSCHED_IDLE\fP, \fBSCHED_BATCH\fP),
104 \fIsched_priority\fP is not used in scheduling
105 decisions (it must be specified as 0).
107 Processes scheduled under one of the real-time policies
108 (\fBSCHED_FIFO\fP, \fBSCHED_RR\fP) have a
109 \fIsched_priority\fP value in the range 1 (low) to 99 (high).
110 (As the numbers imply, real-time threads always have higher priority
111 than normal threads.)
112 Note well: POSIX.1 requires an implementation to support only a
113 minimum 32 distinct priority levels for the real-time policies,
114 and some systems supply just this minimum.
115 Portable programs should use
116 .BR sched_get_priority_min (2)
118 .BR sched_get_priority_max (2)
119 to find the range of priorities supported for a particular policy.
121 Conceptually, the scheduler maintains a list of runnable
122 threads for each possible \fIsched_priority\fP value.
123 In order to determine which thread runs next, the scheduler looks for
124 the nonempty list with the highest static priority and selects the
125 thread at the head of this list.
127 A thread's scheduling policy determines
128 where it will be inserted into the list of threads
129 with equal static priority and how it will move inside this list.
131 All scheduling is preemptive: if a thread with a higher static
132 priority becomes ready to run, the currently running thread
133 will be preempted and
134 returned to the wait list for its static priority level.
135 The scheduling policy determines the
136 ordering only within the list of runnable threads with equal static
138 .SS SCHED_FIFO: First in-first out scheduling
139 \fBSCHED_FIFO\fP can be used only with static priorities higher than
140 0, which means that when a \fBSCHED_FIFO\fP threads becomes runnable,
141 it will always immediately preempt any currently running
142 \fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP thread.
143 \fBSCHED_FIFO\fP is a simple scheduling
144 algorithm without time slicing.
145 For threads scheduled under the
146 \fBSCHED_FIFO\fP policy, the following rules apply:
148 A \fBSCHED_FIFO\fP thread that has been preempted by another thread of
149 higher priority will stay at the head of the list for its priority and
150 will resume execution as soon as all threads of higher priority are
153 When a \fBSCHED_FIFO\fP thread becomes runnable, it
154 will be inserted at the end of the list for its priority.
157 .BR sched_setscheduler (2),
158 .BR sched_setparam (2),
160 .BR sched_setattr (2)
162 \fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) thread identified by
163 \fIpid\fP at the start of the list if it was runnable.
164 As a consequence, it may preempt the currently running thread if
165 it has the same priority.
166 (POSIX.1 specifies that the thread should go to the end
168 .\" In 2.2.x and 2.4.x, the thread is placed at the front of the queue
169 .\" In 2.0.x, the Right Thing happened: the thread went to the back -- MTK
173 will be put at the end of the list.
175 No other events will move a thread
176 scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
177 runnable threads with equal static priority.
180 thread runs until either it is blocked by an I/O request, it is
181 preempted by a higher priority thread, or it calls
183 .SS SCHED_RR: Round-robin scheduling
184 \fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
186 described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
187 except that each thread is allowed to run only for a maximum time
189 If a \fBSCHED_RR\fP thread has been running for a time
190 period equal to or longer than the time quantum, it will be put at the
191 end of the list for its priority.
192 A \fBSCHED_RR\fP thread that has
193 been preempted by a higher priority thread and subsequently resumes
194 execution as a running thread will complete the unexpired portion of
195 its round-robin time quantum.
196 The length of the time quantum can be
198 .BR sched_rr_get_interval (2).
199 .\" On Linux 2.4, the length of the RR interval is influenced
200 .\" by the process nice value -- MTK
202 .SS SCHED_DEADLINE: Sporadic task model deadline scheduling
203 Since version 3.14, Linux provides a deadline scheduling policy
204 .RB ( SCHED_DEADLINE ).
205 This policy is currently implemented using
206 GEDF (Global Earliest Deadline First)
207 in conjunction with CBS (Constant Bandwidth Server).
208 To set and fetch this policy and associated attributes,
209 one must use the Linux-specific
210 .BR sched_setattr (2)
212 .BR sched_getattr (2)
215 A sporadic task is one that has a sequence of jobs, where each
216 job is activated at most once per period.
218 .IR "relative deadline" ,
219 before which it should finish execution, and a
220 .IR "computation time" ,
221 which is the CPU time necessary for executing the job.
222 The moment when a task wakes up
223 because a new job has to be executed is called the
225 (also referred to as the request time or release time).
228 is the time at which a task starts its execution.
230 .I "absolute deadline"
231 is thus obtained by adding the relative deadline to the arrival time.
233 The following diagram clarifies these terms:
237 arrival/wakeup absolute deadline
241 -----x--------xooooooooooooooooo--------x--------x---
243 |<------- relative deadline ------>|
244 |<-------------- period ------------------->|
250 policy for a thread using
251 .BR sched_setattr (2),
252 one can specify three parameters:
257 These parameters do not necessarily correspond to the aforementioned terms:
258 usual practice is to set Runtime to something bigger than the average
259 computation time (or worst-case execution time for hard real-time tasks),
260 Deadline to the relative deadline, and Period to the period of the task.
267 arrival/wakeup absolute deadline
271 -----x--------xooooooooooooooooo--------x--------x---
272 |<-- Runtime ------->|
273 |<----------- Deadline ----------->|
274 |<-------------- Period ------------------->|
278 The three deadline-scheduling parameters correspond to the
286 .BR sched_setattr (2).
287 These fields express values in nanoseconds.
288 .\" FIXME It looks as though specifying sched_period as 0 means
289 .\" "make sched_period the same as sched_deadline".
290 .\" This needs to be documented.
293 is specified as 0, then it is made the same as
296 The kernel requires that:
298 sched_runtime <= sched_deadline <= sched_period
300 .\" See __checkparam_dl in kernel/sched/core.c
301 In addition, under the current implementation,
302 all of the parameter values must be at least 1024
303 (i.e., just over one microsecond,
304 which is the resolution of the implementation), and less than 2^63.
305 If any of these checks fails,
306 .BR sched_setattr (2)
310 The CBS guarantees non-interference between tasks, by throttling
311 threads that attempt to over-run their specified Runtime.
313 To ensure deadline scheduling guarantees,
314 the kernel must prevent situations where the set of
316 threads is not feasible (schedulable) within the given constraints.
317 The kernel thus performs an admittance test when setting or changing
319 policy and attributes.
320 This admission test calculates whether the change is feasible;
322 .BR sched_setattr (2)
326 For example, it is required (but not necessarily sufficient) for
327 the total utilization to be less than or equal to the total number of
328 CPUs available, where, since each thread can maximally run for
329 Runtime per Period, that thread's utilization is its
330 Runtime divided by its Period.
332 In order to fulfill the guarantees that are made when
333 a thread is admitted to the
337 threads are the highest priority (user controllable) threads in the
341 it will preempt any thread scheduled under one of the other policies.
345 by a thread scheduled under the
347 policy will fail with the error
349 unless the thread has its reset-on-fork flag set (see below).
355 will yield the current job and wait for a new period to begin.
357 .\" FIXME Calling sched_getparam() on a SCHED_DEADLINE thread
358 .\" fails with EINVAL, but sched_getscheduler() succeeds.
359 .\" Is that intended? (Why?)
361 .SS SCHED_OTHER: Default Linux time-sharing scheduling
362 \fBSCHED_OTHER\fP can be used at only static priority 0
363 (i.e., threads under real-time policies always have priority over
366 \fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
367 intended for all threads that do not require the special
368 real-time mechanisms.
370 The thread to run is chosen from the static
371 priority 0 list based on a \fIdynamic\fP priority that is determined only
373 The dynamic priority is based on the nice value (set by
377 .BR sched_setattr (2))
378 and is increased for each time quantum the thread is ready to run,
379 but denied to run by the scheduler.
380 This ensures fair progress among all \fBSCHED_OTHER\fP threads.
383 The nice value is a per-process attribute
384 that can be used to influence the CPU scheduler to
385 favor or disfavor a process in scheduling decisions.
386 It affects the scheduling of
392 According to POSIX.1, the threads in a process should share a nice value.
393 However, on Linux, the nice value is a per-thread attribute:
394 different threads in the same process may have different nice values.
396 The range of the nice value
397 varies across UNIX systems.
398 On modern Linux, the range is \-20 (high priority) to +19 (low priority).
399 On some other systems, the range is \-20..20.
400 Very early Linux kernels (Before Linux 2.0) had the range \-infinity..15.
401 .\" Linux before 1.3.36 had \-infinity..15.
402 .\" Since kernel 1.3.43, Linux has the range \-20..19.
404 The degree to which the nice value affects the relative scheduling of
406 processes likewise varies across UNIX systems and
407 across Linux kernel versions.
409 With the advent of the CFS scheduler in kernel 2.6.23,
410 Linux adopted an algorithm that causes
411 relative differences in nice values to have a much stronger effect.
412 In the current implementation, each unit of difference in the
413 nice values of two processes results in a factor of 1.25
414 in the degree to which the scheduler favors the higher priority process.
415 This causes very low nice values (+19) to truly provide little CPU
416 to a process whenever there is any other
417 higher priority load on the system,
418 and makes high nice values (\-20) deliver most of the CPU to applications
419 that require it (e.g., some audio applications).
423 resource limit can be used to define a limit to which
424 an unprivileged process's nice value can be raised; see
428 .SS SCHED_BATCH: Scheduling batch processes
429 (Since Linux 2.6.16.)
430 \fBSCHED_BATCH\fP can be used only at static priority 0.
431 This policy is similar to \fBSCHED_OTHER\fP in that it schedules
432 the thread according to its dynamic priority
433 (based on the nice value).
434 The difference is that this policy
435 will cause the scheduler to always assume
436 that the thread is CPU-intensive.
437 Consequently, the scheduler will apply a small scheduling
438 penalty with respect to wakeup behavior,
439 so that this thread is mildly disfavored in scheduling decisions.
441 .\" The following paragraph is drawn largely from the text that
442 .\" accompanied Ingo Molnar's patch for the implementation of
444 .\" commit b0a9499c3dd50d333e2aedb7e894873c58da3785
445 This policy is useful for workloads that are noninteractive,
446 but do not want to lower their nice value,
447 and for workloads that want a deterministic scheduling policy without
448 interactivity causing extra preemptions (between the workload's tasks).
450 .SS SCHED_IDLE: Scheduling very low priority jobs
451 (Since Linux 2.6.23.)
452 \fBSCHED_IDLE\fP can be used only at static priority 0;
453 the process nice value has no influence for this policy.
455 This policy is intended for running jobs at extremely low
456 priority (lower even than a +19 nice value with the
462 .SS Resetting scheduling policy for child processes
463 Each thread has a reset-on-fork scheduling flag.
464 When this flag is set, children created by
466 do not inherit privileged scheduling policies.
467 The reset-on-fork flag can be set by either:
470 .B SCHED_RESET_ON_FORK
473 argument when calling
474 .BR sched_setscheduler (2)
475 (since Linux 2.6.32);
479 .B SCHED_FLAG_RESET_ON_FORK
483 .BR sched_setattr (2).
485 Note that the constants used with these two APIs have different names.
486 The state of the reset-on-fork flag can analogously be retrieved using
487 .BR sched_getscheduler (2)
489 .BR sched_getattr (2).
491 The reset-on-fork feature is intended for media-playback applications,
492 and can be used to prevent applications evading the
496 by creating multiple child processes.
498 More precisely, if the reset-on-fork flag is set,
499 the following rules apply for subsequently created children:
501 If the calling thread has a scheduling policy of
505 the policy is reset to
509 If the calling process has a negative nice value,
510 the nice value is reset to zero in child processes.
512 After the reset-on-fork flag has been enabled,
513 it can be reset only if the thread has the
516 This flag is disabled in child processes created by
519 .SS Privileges and resource limits
520 In Linux kernels before 2.6.12, only privileged
522 threads can set a nonzero static priority (i.e., set a real-time
524 The only change that an unprivileged thread can make is to set the
526 policy, and this can be done only if the effective user ID of the caller
527 matches the real or effective user ID of the target thread
528 (i.e., the thread specified by
530 whose policy is being changed.
532 A thread must be privileged
534 in order to set or modify a
538 Since Linux 2.6.12, the
540 resource limit defines a ceiling on an unprivileged thread's
541 static priority for the
546 The rules for changing scheduling policy and priority are as follows:
548 If an unprivileged thread has a nonzero
550 soft limit, then it can change its scheduling policy and priority,
551 subject to the restriction that the priority cannot be set to a
552 value higher than the maximum of its current priority and its
558 soft limit is 0, then the only permitted changes are to lower the priority,
559 or to switch to a non-real-time policy.
561 Subject to the same rules,
562 another unprivileged thread can also make these changes,
563 as long as the effective user ID of the thread making the change
564 matches the real or effective user ID of the target thread.
566 Special rules apply for the
569 In Linux kernels before 2.6.39,
570 an unprivileged thread operating under this policy cannot
571 change its policy, regardless of the value of its
574 In Linux kernels since 2.6.39,
575 .\" commit c02aa73b1d18e43cfd79c2f193b225e84ca497c8
576 an unprivileged thread can switch to either the
580 policy so long as its nice value falls within the range permitted by its
589 limit; as with older kernels,
590 they can make arbitrary changes to scheduling policy and priority.
593 for further information on
595 .SS Limiting the CPU usage of real-time and deadline processes
596 A nonblocking infinite loop in a thread scheduled under the
601 policy can potentially block all other threads from accessing
603 Prior to Linux 2.6.25, the only way of preventing a runaway real-time
604 process from freezing the system was to run (at the console)
605 a shell scheduled under a higher static priority than the tested application.
606 This allows an emergency kill of tested
607 real-time applications that do not block or terminate as expected.
609 Since Linux 2.6.25, there are other techniques for dealing with runaway
610 real-time and deadline processes.
611 One of these is to use the
613 resource limit to set a ceiling on the CPU time that
614 a real-time process may consume.
619 Since version 2.6.25, Linux also provides two
621 files that can be used to reserve a certain amount of CPU time
622 to be used by non-real-time processes.
623 Reserving CPU time in this fashion allows some CPU time to be
624 allocated to (say) a root shell that can be used to kill a runaway process.
625 Both of these files specify time values in microseconds:
627 .IR /proc/sys/kernel/sched_rt_period_us
628 This file specifies a scheduling period that is equivalent to
630 The value in this file can range from 1 to
632 giving an operating range of 1 microsecond to around 35 minutes.
633 The default value in this file is 1,000,000 (1 second).
635 .IR /proc/sys/kernel/sched_rt_runtime_us
636 The value in this file specifies how much of the "period" time
637 can be used by all real-time and deadline scheduled processes
639 The value in this file can range from \-1 to
641 Specifying \-1 makes the runtime the same as the period;
642 that is, no CPU time is set aside for non-real-time processes
643 (which was the Linux behavior before kernel 2.6.25).
644 The default value in this file is 950,000 (0.95 seconds),
645 meaning that 5% of the CPU time is reserved for processes that
646 don't run under a real-time or deadline scheduling policy.
649 A blocked high priority thread waiting for I/O has a certain
650 response time before it is scheduled again.
651 The device driver writer
652 can greatly reduce this response time by using a "slow interrupt"
655 .\" .BR request_irq (9).
657 Child processes inherit the scheduling policy and parameters across a
659 The scheduling policy and parameters are preserved across
662 Memory locking is usually needed for real-time processes to avoid
663 paging delays; this can be done with
670 CPU controller can be used to limit the CPU consumption of
673 Originally, Standard Linux was intended as a general-purpose operating
674 system being able to handle background processes, interactive
675 applications, and less demanding real-time applications (applications that
676 need to usually meet timing deadlines).
677 Although the Linux kernel 2.6
678 allowed for kernel preemption and the newly introduced O(1) scheduler
679 ensures that the time needed to schedule is fixed and deterministic
680 irrespective of the number of active tasks, true real-time computing
681 was not possible up to kernel version 2.6.17.
682 .SS Real-time features in the mainline Linux kernel
683 .\" FIXME . Probably this text will need some minor tweaking
684 .\" ask Carsten Emde about this.
685 From kernel version 2.6.18 onward, however, Linux is gradually
686 becoming equipped with real-time capabilities,
687 most of which are derived from the former
690 Until the patches have been completely merged into the
692 they must be installed to achieve the best real-time performance.
693 These patches are named:
697 patch-\fIkernelversion\fP-rt\fIpatchversion\fP
701 and can be downloaded from
702 .UR http://www.kernel.org\:/pub\:/linux\:/kernel\:/projects\:/rt/
705 Without the patches and prior to their full inclusion into the mainline
706 kernel, the kernel configuration offers only the three preemption classes
707 .BR CONFIG_PREEMPT_NONE ,
708 .BR CONFIG_PREEMPT_VOLUNTARY ,
710 .B CONFIG_PREEMPT_DESKTOP
711 which respectively provide no, some, and considerable
712 reduction of the worst-case scheduling latency.
714 With the patches applied or after their full inclusion into the mainline
715 kernel, the additional configuration item
718 If this is selected, Linux is transformed into a regular
719 real-time operating system.
720 The FIFO and RR scheduling policies are then used to run a thread
721 with true real-time priority and a minimum worst-case scheduling latency.
733 .BR sched_get_priority_max (2),
734 .BR sched_get_priority_min (2),
735 .BR sched_getaffinity (2),
736 .BR sched_getparam (2),
737 .BR sched_getscheduler (2),
738 .BR sched_rr_get_interval (2),
739 .BR sched_setaffinity (2),
740 .BR sched_setparam (2),
741 .BR sched_setscheduler (2),
744 .BR pthread_getaffinity_np (3),
745 .BR pthread_setaffinity_np (3),
746 .BR sched_getcpu (3),
747 .BR capabilities (7),
751 .I Programming for the real world \- POSIX.4
752 by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0.
754 The Linux kernel source files
755 .IR Documentation/scheduler/sched-deadline.txt ,
756 .IR Documentation/scheduler/sched-rt-group.txt ,
757 .IR Documentation/scheduler/sched-design-CFS.txt ,
759 .IR Documentation/scheduler/sched-nice-design.txt