1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (C) Tom Bjorkholm, Markus Kuhn & David A. Wheeler 1996-1999
4 .\" and Copyright (C) 2007 Carsten Emde <Carsten.Emde@osadl.org>
5 .\" and Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU General Public License for more details.
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, write to the Free
24 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
27 .\" 1996-04-01 Tom Bjorkholm <tomb@mydata.se>
28 .\" First version written
29 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
31 .\" 1999-08-18 David A. Wheeler <dwheeler@ida.org> added Note.
32 .\" Modified, 25 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Corrected description of queue placement by sched_setparam() and
34 .\" sched_setscheduler()
35 .\" A couple of grammar clean-ups
36 .\" Modified 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2005-03-23, mtk, Added description of SCHED_BATCH.
38 .\" 2007-07-10, Carsten Emde <Carsten.Emde@osadl.org>
39 .\" Add text on real-time features that are currently being
40 .\" added to the mainline kernel.
41 .\" 2008-05-07, mtk; Rewrote and restructured various parts of the page to
42 .\" improve readability.
43 .\" 2010-06-19, mtk, documented SCHED_RESET_ON_FORK
45 .\" Worth looking at: http://rt.wiki.kernel.org/index.php
47 .TH SCHED_SETSCHEDULER 2 2010-10-04 "Linux" "Linux Programmer's Manual"
49 sched_setscheduler, sched_getscheduler \-
50 set and get scheduling policy/parameters
55 .BI "int sched_setscheduler(pid_t " pid ", int " policy ,
57 .BI " const struct sched_param *" param );
59 .BI "int sched_getscheduler(pid_t " pid );
61 \fBstruct sched_param {
63 int \fIsched_priority\fB;
68 .BR sched_setscheduler ()
69 sets both the scheduling policy and the associated parameters for the
70 process whose ID is specified in \fIpid\fP.
71 If \fIpid\fP equals zero, the
72 scheduling policy and parameters of the calling process will be set.
74 the argument \fIparam\fP depends on the selected policy.
75 Currently, Linux supports the following "normal"
76 (i.e., non-real-time) scheduling policies:
79 the standard round-robin time-sharing policy;
80 .\" In the 2.6 kernel sources, SCHED_OTHER is actually called
84 for "batch" style execution of processes; and
89 low priority background jobs.
91 The following "real-time" policies are also supported,
92 for special time-critical applications that need precise control over
93 the way in which runnable processes are selected for execution:
96 a first-in, first-out policy; and
101 The semantics of each of these policies are detailed below.
103 .BR sched_getscheduler ()
104 queries the scheduling policy currently applied to the process
105 identified by \fIpid\fP.
106 If \fIpid\fP equals zero, the policy of the
107 calling process will be retrieved.
109 .SS Scheduling Policies
110 The scheduler is the kernel component that decides which runnable process
111 will be executed by the CPU next.
112 Each process has an associated scheduling policy and a \fIstatic\fP
113 scheduling priority, \fIsched_priority\fP; these are the settings
115 .BR sched_setscheduler ().
116 The scheduler makes it decisions based on knowledge of the scheduling
117 policy and static priority of all processes on the system.
119 For processes scheduled under one of the normal scheduling policies
120 (\fBSCHED_OTHER\fP, \fBSCHED_IDLE\fP, \fBSCHED_BATCH\fP),
121 \fIsched_priority\fP is not used in scheduling
122 decisions (it must be specified as 0).
124 Processes scheduled under one of the real-time policies
125 (\fBSCHED_FIFO\fP, \fBSCHED_RR\fP) have a
126 \fIsched_priority\fP value in the range 1 (low) to 99 (high).
127 (As the numbers imply, real-time processes always have higher priority
128 than normal processes.)
129 Note well: POSIX.1-2001 only requires an implementation to support a
130 minimum 32 distinct priority levels for the real-time policies,
131 and some systems supply just this minimum.
132 Portable programs should use
133 .BR sched_get_priority_min (2)
135 .BR sched_get_priority_max (2)
136 to find the range of priorities supported for a particular policy.
138 Conceptually, the scheduler maintains a list of runnable
139 processes for each possible \fIsched_priority\fP value.
140 In order to determine which process runs next, the scheduler looks for
141 the nonempty list with the highest static priority and selects the
142 process at the head of this list.
144 A process's scheduling policy determines
145 where it will be inserted into the list of processes
146 with equal static priority and how it will move inside this list.
148 All scheduling is preemptive: if a process with a higher static
149 priority becomes ready to run, the currently running process
150 will be preempted and
151 returned to the wait list for its static priority level.
152 The scheduling policy only determines the
153 ordering within the list of runnable processes with equal static
155 .SS SCHED_FIFO: First In-First Out scheduling
156 \fBSCHED_FIFO\fP can only be used with static priorities higher than
157 0, which means that when a \fBSCHED_FIFO\fP processes becomes runnable,
158 it will always immediately preempt any currently running
159 \fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP process.
160 \fBSCHED_FIFO\fP is a simple scheduling
161 algorithm without time slicing.
162 For processes scheduled under the
163 \fBSCHED_FIFO\fP policy, the following rules apply:
165 A \fBSCHED_FIFO\fP process that has been preempted by another process of
166 higher priority will stay at the head of the list for its priority and
167 will resume execution as soon as all processes of higher priority are
170 When a \fBSCHED_FIFO\fP process becomes runnable, it
171 will be inserted at the end of the list for its priority.
174 .BR sched_setscheduler ()
176 .BR sched_setparam (2)
178 \fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) process identified by
179 \fIpid\fP at the start of the list if it was runnable.
180 As a consequence, it may preempt the currently running process if
181 it has the same priority.
182 (POSIX.1-2001 specifies that the process should go to the end
184 .\" In 2.2.x and 2.4.x, the process is placed at the front of the queue
185 .\" In 2.0.x, the Right Thing happened: the process went to the back -- MTK
189 will be put at the end of the list.
191 No other events will move a process
192 scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
193 runnable processes with equal static priority.
196 process runs until either it is blocked by an I/O request, it is
197 preempted by a higher priority process, or it calls
199 .SS SCHED_RR: Round Robin scheduling
200 \fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
202 described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
203 except that each process is only allowed to run for a maximum time
205 If a \fBSCHED_RR\fP process has been running for a time
206 period equal to or longer than the time quantum, it will be put at the
207 end of the list for its priority.
208 A \fBSCHED_RR\fP process that has
209 been preempted by a higher priority process and subsequently resumes
210 execution as a running process will complete the unexpired portion of
211 its round robin time quantum.
212 The length of the time quantum can be
214 .BR sched_rr_get_interval (2).
215 .\" On Linux 2.4, the length of the RR interval is influenced
216 .\" by the process nice value -- MTK
218 .SS SCHED_OTHER: Default Linux time-sharing scheduling
219 \fBSCHED_OTHER\fP can only be used at static priority 0.
220 \fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
221 intended for all processes that do not require the special
222 real-time mechanisms.
223 The process to run is chosen from the static
224 priority 0 list based on a \fIdynamic\fP priority that is determined only
226 The dynamic priority is based on the nice value (set by
230 and increased for each time quantum the process is ready to run,
231 but denied to run by the scheduler.
232 This ensures fair progress among all \fBSCHED_OTHER\fP processes.
234 .SS SCHED_BATCH: Scheduling batch processes
235 (Since Linux 2.6.16.)
236 \fBSCHED_BATCH\fP can only be used at static priority 0.
237 This policy is similar to \fBSCHED_OTHER\fP in that it schedules
238 the process according to its dynamic priority
239 (based on the nice value).
240 The difference is that this policy
241 will cause the scheduler to always assume
242 that the process is CPU-intensive.
243 Consequently, the scheduler will apply a small scheduling
244 penalty with respect to wakeup behaviour,
245 so that this process is mildly disfavored in scheduling decisions.
247 .\" The following paragraph is drawn largely from the text that
248 .\" accompanied Ingo Molnar's patch for the implementation of
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 only be used 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 process 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 only be reset if the process 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 processes can set a nonzero static priority (i.e., set a real-time
319 The only change that an unprivileged process can make is to set the
321 policy, and this can only be done if the effective user ID of the caller of
322 .BR sched_setscheduler ()
323 matches the real or effective user ID of the target process
324 (i.e., the process specified by
326 whose policy is being changed.
328 Since Linux 2.6.12, the
330 resource limit defines a ceiling on an unprivileged process's
331 static priority for the
336 The rules for changing scheduling policy and priority are as follows:
338 If an unprivileged process 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 process can also make these changes,
353 as long as the effective user ID of the process making the change
354 matches the real or effective user ID of the target process.
356 Special rules apply for the
358 an unprivileged process operating under this policy cannot
359 change its policy, regardless of the value of its
367 limit; as with older kernels,
368 they can make arbitrary changes to scheduling policy and priority.
371 for further information on
374 A blocked high priority process waiting for the I/O has a certain
375 response time before it is scheduled again.
376 The device driver writer
377 can greatly reduce this response time by using a "slow interrupt"
380 .\" .BR request_irq (9).
382 Child processes inherit the scheduling policy and parameters across a
384 The scheduling policy and parameters are preserved across
387 Memory locking is usually needed for real-time processes to avoid
388 paging delays; this can be done with
393 Since a nonblocking infinite loop in a process scheduled under
394 \fBSCHED_FIFO\fP or \fBSCHED_RR\fP will block all processes with lower
395 priority forever, a software developer should always keep available on
396 the console a shell scheduled under a higher static priority than the
398 This will allow an emergency kill of tested
399 real-time applications that do not block or terminate as expected.
400 See also the description of the
405 POSIX systems on which
406 .BR sched_setscheduler ()
408 .BR sched_getscheduler ()
410 .B _POSIX_PRIORITY_SCHEDULING
414 .BR sched_setscheduler ()
417 .BR sched_getscheduler ()
418 returns the policy for the process (a nonnegative integer).
419 On error, \-1 is returned, and
421 is set appropriately.
425 The scheduling \fIpolicy\fP is not one of the recognized policies,
427 or \fIparam\fP does not make sense for the \fIpolicy\fP.
430 The calling process does not have appropriate privileges.
433 The process whose ID is \fIpid\fP could not be found.
435 POSIX.1-2001 (but see BUGS below).
436 The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
438 POSIX.1 does not detail the permissions that an unprivileged
439 process requires in order to call
440 .BR sched_setscheduler (),
441 and details vary across systems.
442 For example, the Solaris 7 manual page says that
443 the real or effective user ID of the calling process must
444 match the real user ID or the save set-user-ID of the target process.
446 Originally, Standard Linux was intended as a general-purpose operating
447 system being able to handle background processes, interactive
448 applications, and less demanding real-time applications (applications that
449 need to usually meet timing deadlines).
450 Although the Linux kernel 2.6
451 allowed for kernel preemption and the newly introduced O(1) scheduler
452 ensures that the time needed to schedule is fixed and deterministic
453 irrespective of the number of active tasks, true real-time computing
454 was not possible up to kernel version 2.6.17.
455 .SS Real-time features in the mainline Linux kernel
456 .\" FIXME . Probably this text will need some minor tweaking
457 .\" by about the time of 2.6.30; ask Carsten Emde about this then.
458 From kernel version 2.6.18 onward, however, Linux is gradually
459 becoming equipped with real-time capabilities,
460 most of which are derived from the former
462 patches developed by Ingo Molnar, Thomas Gleixner,
463 Steven Rostedt, and others.
464 Until the patches have been completely merged into the
466 (this is expected to be around kernel version 2.6.30),
467 they must be installed to achieve the best real-time performance.
468 These patches are named:
472 patch-\fIkernelversion\fP-rt\fIpatchversion\fP
476 and can be downloaded from
477 .IR http://www.kernel.org/pub/linux/kernel/projects/rt/ .
479 Without the patches and prior to their full inclusion into the mainline
480 kernel, the kernel configuration offers only the three preemption classes
481 .BR CONFIG_PREEMPT_NONE ,
482 .BR CONFIG_PREEMPT_VOLUNTARY ,
484 .B CONFIG_PREEMPT_DESKTOP
485 which respectively provide no, some, and considerable
486 reduction of the worst-case scheduling latency.
488 With the patches applied or after their full inclusion into the mainline
489 kernel, the additional configuration item
492 If this is selected, Linux is transformed into a regular
493 real-time operating system.
494 The FIFO and RR scheduling policies that can be selected using
495 .BR sched_setscheduler ()
496 are then used to run a process
497 with true real-time priority and a minimum worst-case scheduling latency.
499 POSIX says that on success,
500 .BR sched_setscheduler ()
501 should return the previous scheduling policy.
503 .BR sched_setscheduler ()
504 does not conform to this requirement,
505 since it always returns 0 on success.
513 .BR sched_get_priority_max (2),
514 .BR sched_get_priority_min (2),
515 .BR sched_getaffinity (2),
516 .BR sched_getparam (2),
517 .BR sched_rr_get_interval (2),
518 .BR sched_setaffinity (2),
519 .BR sched_setparam (2),
522 .BR capabilities (7),
525 .I Programming for the real world \- POSIX.4
526 by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0
528 The kernel source file
529 .I Documentation/scheduler/sched-rt-group.txt
530 (since kernel 2.6.25).