1 .\" Copyright (C) Tom Bjorkholm, Markus Kuhn & David A. Wheeler 1996-1999
2 .\" and Copyright (C) 2007 Carsten Emde <Carsten.Emde@osadl.org>
3 .\" and Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
26 .\" 1996-04-01 Tom Bjorkholm <tomb@mydata.se>
27 .\" First version written
28 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
30 .\" 1999-08-18 David A. Wheeler <dwheeler@ida.org> added Note.
31 .\" Modified, 25 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Corrected description of queue placement by sched_setparam() and
33 .\" sched_setscheduler()
34 .\" A couple of grammar clean-ups
35 .\" Modified 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
36 .\" 2005-03-23, mtk, Added description of SCHED_BATCH.
37 .\" 2007-07-10, Carsten Emde <Carsten.Emde@osadl.org>
38 .\" Add text on real-time features that are currently being
39 .\" added to the mainline kernel.
40 .\" 2008-05-07, mtk; Rewrote and restructured various parts of the page to
41 .\" improve readability.
42 .\" 2010-06-19, mtk, documented SCHED_RESET_ON_FORK
44 .\" Worth looking at: http://rt.wiki.kernel.org/index.php
46 .TH SCHED_SETSCHEDULER 2 2013-09-17 "Linux" "Linux Programmer's Manual"
48 sched_setscheduler, sched_getscheduler \-
49 set and get scheduling policy/parameters
54 .BI "int sched_setscheduler(pid_t " pid ", int " policy ,
56 .BI " const struct sched_param *" param );
58 .BI "int sched_getscheduler(pid_t " pid );
60 \fBstruct sched_param {
62 int \fIsched_priority\fB;
67 .BR sched_setscheduler ()
68 sets both the scheduling policy and the associated parameters for the
69 thread whose ID is specified in \fIpid\fP.
70 If \fIpid\fP equals zero, the
71 scheduling policy and parameters of the calling thread will be set.
73 the argument \fIparam\fP depends on the selected policy.
74 Currently, Linux supports the following "normal"
75 (i.e., non-real-time) scheduling policies:
78 the standard round-robin time-sharing policy;
79 .\" In the 2.6 kernel sources, SCHED_OTHER is actually called
83 for "batch" style execution of processes; and
88 low priority background jobs.
90 The following "real-time" policies are also supported,
91 for special time-critical applications that need precise control over
92 the way in which runnable threads are selected for execution:
95 a first-in, first-out policy; and
100 The semantics of each of these policies are detailed below.
102 .BR sched_getscheduler ()
103 queries the scheduling policy currently applied to the thread
104 identified by \fIpid\fP.
105 If \fIpid\fP equals zero, the policy of the
106 calling thread will be retrieved.
108 .SS Scheduling policies
109 The scheduler is the kernel component that decides which runnable thread
110 will be executed by the CPU next.
111 Each thread has an associated scheduling policy and a \fIstatic\fP
112 scheduling priority, \fIsched_priority\fP; these are the settings
114 .BR sched_setscheduler ().
115 The scheduler makes it decisions based on knowledge of the scheduling
116 policy and static priority of all threads on the system.
118 For threads scheduled under one of the normal scheduling policies
119 (\fBSCHED_OTHER\fP, \fBSCHED_IDLE\fP, \fBSCHED_BATCH\fP),
120 \fIsched_priority\fP is not used in scheduling
121 decisions (it must be specified as 0).
123 Processes scheduled under one of the real-time policies
124 (\fBSCHED_FIFO\fP, \fBSCHED_RR\fP) have a
125 \fIsched_priority\fP value in the range 1 (low) to 99 (high).
126 (As the numbers imply, real-time threads always have higher priority
127 than normal threads.)
128 Note well: POSIX.1-2001 requires an implementation to support only a
129 minimum 32 distinct priority levels for the real-time policies,
130 and some systems supply just this minimum.
131 Portable programs should use
132 .BR sched_get_priority_min (2)
134 .BR sched_get_priority_max (2)
135 to find the range of priorities supported for a particular policy.
137 Conceptually, the scheduler maintains a list of runnable
138 threads for each possible \fIsched_priority\fP value.
139 In order to determine which thread runs next, the scheduler looks for
140 the nonempty list with the highest static priority and selects the
141 thread at the head of this list.
143 A thread's scheduling policy determines
144 where it will be inserted into the list of threads
145 with equal static priority and how it will move inside this list.
147 All scheduling is preemptive: if a thread with a higher static
148 priority becomes ready to run, the currently running thread
149 will be preempted and
150 returned to the wait list for its static priority level.
151 The scheduling policy determines the
152 ordering only within the list of runnable threads with equal static
154 .SS SCHED_FIFO: First in-first out scheduling
155 \fBSCHED_FIFO\fP can be used only with static priorities higher than
156 0, which means that when a \fBSCHED_FIFO\fP threads becomes runnable,
157 it will always immediately preempt any currently running
158 \fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP thread.
159 \fBSCHED_FIFO\fP is a simple scheduling
160 algorithm without time slicing.
161 For threads scheduled under the
162 \fBSCHED_FIFO\fP policy, the following rules apply:
164 A \fBSCHED_FIFO\fP thread that has been preempted by another thread of
165 higher priority will stay at the head of the list for its priority and
166 will resume execution as soon as all threads of higher priority are
169 When a \fBSCHED_FIFO\fP thread becomes runnable, it
170 will be inserted at the end of the list for its priority.
173 .BR sched_setscheduler ()
175 .BR sched_setparam (2)
177 \fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) thread identified by
178 \fIpid\fP at the start of the list if it was runnable.
179 As a consequence, it may preempt the currently running thread if
180 it has the same priority.
181 (POSIX.1-2001 specifies that the thread should go to the end
183 .\" In 2.2.x and 2.4.x, the thread is placed at the front of the queue
184 .\" In 2.0.x, the Right Thing happened: the thread went to the back -- MTK
188 will be put at the end of the list.
190 No other events will move a thread
191 scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
192 runnable threads with equal static priority.
195 thread runs until either it is blocked by an I/O request, it is
196 preempted by a higher priority thread, or it calls
198 .SS SCHED_RR: Round-robin scheduling
199 \fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
201 described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
202 except that each thread is allowed to run only for a maximum time
204 If a \fBSCHED_RR\fP thread has been running for a time
205 period equal to or longer than the time quantum, it will be put at the
206 end of the list for its priority.
207 A \fBSCHED_RR\fP thread that has
208 been preempted by a higher priority thread and subsequently resumes
209 execution as a running thread will complete the unexpired portion of
210 its round-robin time quantum.
211 The length of the time quantum can be
213 .BR sched_rr_get_interval (2).
214 .\" On Linux 2.4, the length of the RR interval is influenced
215 .\" by the process nice value -- MTK
217 .SS SCHED_OTHER: Default Linux time-sharing scheduling
218 \fBSCHED_OTHER\fP can be used at only static priority 0.
219 \fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
220 intended for all threads that do not require the special
221 real-time mechanisms.
222 The thread to run is chosen from the static
223 priority 0 list based on a \fIdynamic\fP priority that is determined only
225 The dynamic priority is based on the nice value (set by
229 and increased for each time quantum the thread is ready to run,
230 but denied to run by the scheduler.
231 This ensures fair progress among all \fBSCHED_OTHER\fP threads.
233 .SS SCHED_BATCH: Scheduling batch processes
234 (Since Linux 2.6.16.)
235 \fBSCHED_BATCH\fP can be used only at static priority 0.
236 This policy is similar to \fBSCHED_OTHER\fP in that it schedules
237 the thread according to its dynamic priority
238 (based on the nice value).
239 The difference is that this policy
240 will cause the scheduler to always assume
241 that the thread is CPU-intensive.
242 Consequently, the scheduler will apply a small scheduling
243 penalty with respect to wakeup behaviour,
244 so that this thread is mildly disfavored in scheduling decisions.
246 .\" The following paragraph is drawn largely from the text that
247 .\" accompanied Ingo Molnar's patch for the implementation of
249 .\" commit b0a9499c3dd50d333e2aedb7e894873c58da3785
250 This policy is useful for workloads that are noninteractive,
251 but do not want to lower their nice value,
252 and for workloads that want a deterministic scheduling policy without
253 interactivity causing extra preemptions (between the workload's tasks).
255 .SS SCHED_IDLE: Scheduling very low priority jobs
256 (Since Linux 2.6.23.)
257 \fBSCHED_IDLE\fP can be used only at static priority 0;
258 the process nice value has no influence for this policy.
260 This policy is intended for running jobs at extremely low
261 priority (lower even than a +19 nice value with the
267 .SS Resetting scheduling policy for child processes
268 Since Linux 2.6.32, the
269 .B SCHED_RESET_ON_FORK
273 .BR sched_setscheduler ().
274 As a result of including this flag, children created by
276 do not inherit privileged scheduling policies.
277 This feature is intended for media-playback applications,
278 and can be used to prevent applications evading the
282 by creating multiple child processes.
284 More precisely, if the
285 .BR SCHED_RESET_ON_FORK
287 the following rules apply for subsequently created children:
289 If the calling thread has a scheduling policy of
293 the policy is reset to
297 If the calling process has a negative nice value,
298 the nice value is reset to zero in child processes.
301 .BR SCHED_RESET_ON_FORK
302 flag has been enabled,
303 it can be reset only if the thread has the
306 This flag is disabled in child processes created by
310 .B SCHED_RESET_ON_FORK
311 flag is visible in the policy value returned by
312 .BR sched_getscheduler ()
314 .SS Privileges and resource limits
315 In Linux kernels before 2.6.12, only privileged
317 threads can set a nonzero static priority (i.e., set a real-time
319 The only change that an unprivileged thread can make is to set the
321 policy, and this can be done only if the effective user ID of the caller of
322 .BR sched_setscheduler ()
323 matches the real or effective user ID of the target thread
324 (i.e., the thread specified by
326 whose policy is being changed.
328 Since Linux 2.6.12, the
330 resource limit defines a ceiling on an unprivileged thread's
331 static priority for the
336 The rules for changing scheduling policy and priority are as follows:
338 If an unprivileged thread has a nonzero
340 soft limit, then it can change its scheduling policy and priority,
341 subject to the restriction that the priority cannot be set to a
342 value higher than the maximum of its current priority and its
348 soft limit is 0, then the only permitted changes are to lower the priority,
349 or to switch to a non-real-time policy.
351 Subject to the same rules,
352 another unprivileged thread can also make these changes,
353 as long as the effective user ID of the thread making the change
354 matches the real or effective user ID of the target thread.
356 Special rules apply for the
358 In Linux kernels before 2.6.39,
359 an unprivileged thread operating under this policy cannot
360 change its policy, regardless of the value of its
363 In Linux kernels since 2.6.39,
364 .\" commit c02aa73b1d18e43cfd79c2f193b225e84ca497c8
365 an unprivileged thread can switch to either the
369 policy so long as its nice value falls within the range permitted by its
378 limit; as with older kernels,
379 they can make arbitrary changes to scheduling policy and priority.
382 for further information on
385 A blocked high priority thread waiting for the I/O has a certain
386 response time before it is scheduled again.
387 The device driver writer
388 can greatly reduce this response time by using a "slow interrupt"
391 .\" .BR request_irq (9).
393 Child processes inherit the scheduling policy and parameters across a
395 The scheduling policy and parameters are preserved across
398 Memory locking is usually needed for real-time processes to avoid
399 paging delays; this can be done with
404 Since a nonblocking infinite loop in a thread scheduled under
405 \fBSCHED_FIFO\fP or \fBSCHED_RR\fP will block all threads with lower
406 priority forever, a software developer should always keep available on
407 the console a shell scheduled under a higher static priority than the
409 This will allow an emergency kill of tested
410 real-time applications that do not block or terminate as expected.
411 See also the description of the
416 POSIX systems on which
417 .BR sched_setscheduler ()
419 .BR sched_getscheduler ()
421 .B _POSIX_PRIORITY_SCHEDULING
425 .BR sched_setscheduler ()
428 .BR sched_getscheduler ()
429 returns the policy for the thread (a nonnegative integer).
430 On error, \-1 is returned, and
432 is set appropriately.
436 The scheduling \fIpolicy\fP is not one of the recognized policies,
438 or \fIparam\fP does not make sense for the \fIpolicy\fP.
441 The calling thread does not have appropriate privileges.
444 The thread whose ID is \fIpid\fP could not be found.
446 POSIX.1-2001 (but see BUGS below).
447 The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
449 POSIX.1 does not detail the permissions that an unprivileged
450 thread requires in order to call
451 .BR sched_setscheduler (),
452 and details vary across systems.
453 For example, the Solaris 7 manual page says that
454 the real or effective user ID of the caller must
455 match the real user ID or the save set-user-ID of the target.
457 The scheduling policy and parameters are in fact per-thread
459 The value returned from a call to
461 can be passed in the argument
465 as 0 will operate on the attribute for the calling thread,
466 and passing the value returned from a call to
468 will operate on the attribute for the main thread of the thread group.
469 (If you are using the POSIX threads API, then use
470 .BR pthread_setschedparam (3),
471 .BR pthread_getschedparam (3),
473 .BR pthread_setschedprio (3),
478 Originally, Standard Linux was intended as a general-purpose operating
479 system being able to handle background processes, interactive
480 applications, and less demanding real-time applications (applications that
481 need to usually meet timing deadlines).
482 Although the Linux kernel 2.6
483 allowed for kernel preemption and the newly introduced O(1) scheduler
484 ensures that the time needed to schedule is fixed and deterministic
485 irrespective of the number of active tasks, true real-time computing
486 was not possible up to kernel version 2.6.17.
487 .SS Real-time features in the mainline Linux kernel
488 .\" FIXME . Probably this text will need some minor tweaking
489 .\" by about the time of 2.6.30; ask Carsten Emde about this then.
490 From kernel version 2.6.18 onward, however, Linux is gradually
491 becoming equipped with real-time capabilities,
492 most of which are derived from the former
494 patches developed by Ingo Molnar, Thomas Gleixner,
495 Steven Rostedt, and others.
496 Until the patches have been completely merged into the
498 (this is expected to be around kernel version 2.6.30),
499 they must be installed to achieve the best real-time performance.
500 These patches are named:
504 patch-\fIkernelversion\fP-rt\fIpatchversion\fP
508 and can be downloaded from
509 .UR http://www.kernel.org\:/pub\:/linux\:/kernel\:/projects\:/rt/
512 Without the patches and prior to their full inclusion into the mainline
513 kernel, the kernel configuration offers only the three preemption classes
514 .BR CONFIG_PREEMPT_NONE ,
515 .BR CONFIG_PREEMPT_VOLUNTARY ,
517 .B CONFIG_PREEMPT_DESKTOP
518 which respectively provide no, some, and considerable
519 reduction of the worst-case scheduling latency.
521 With the patches applied or after their full inclusion into the mainline
522 kernel, the additional configuration item
525 If this is selected, Linux is transformed into a regular
526 real-time operating system.
527 The FIFO and RR scheduling policies that can be selected using
528 .BR sched_setscheduler ()
529 are then used to run a thread
530 with true real-time priority and a minimum worst-case scheduling latency.
532 POSIX says that on success,
533 .BR sched_setscheduler ()
534 should return the previous scheduling policy.
536 .BR sched_setscheduler ()
537 does not conform to this requirement,
538 since it always returns 0 on success.
549 .BR sched_get_priority_max (2),
550 .BR sched_get_priority_min (2),
551 .BR sched_getaffinity (2),
552 .BR sched_getparam (2),
553 .BR sched_rr_get_interval (2),
554 .BR sched_setaffinity (2),
555 .BR sched_setparam (2),
558 .BR capabilities (7),
562 .I Programming for the real world \- POSIX.4
563 by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0.
565 The Linux kernel source file
566 .I Documentation/scheduler/sched-rt-group.txt
568 This page is part of release 3.64 of the Linux
571 A description of the project,
572 and information about reporting bugs,
574 \%http://www.kernel.org/doc/man\-pages/.