]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/sched_setscheduler.2
_syscall.2, accept.2, epoll_create.2, inotify_add_watch.2, ioctl.2, msgget.2, msgop...
[thirdparty/man-pages.git] / man2 / sched_setscheduler.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" Copyright (C) Tom Bjorkholm, Markus Kuhn & David A. Wheeler 1996-1999
cf9c27a6 4.\" and Copyright (C) 2007 Carsten Emde <Carsten.Emde@osadl.org>
a3a22b7f 5.\" and Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da
MK
6.\"
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.
11.\"
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.
16.\"
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.
21.\"
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,
25.\" USA.
26.\"
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>
30.\" revision
31.\" 1999-08-18 David A. Wheeler <dwheeler@ida.org> added Note.
c11b1abf 32.\" Modified, 25 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
c13182ef 33.\" Corrected description of queue placement by sched_setparam() and
fea681da
MK
34.\" sched_setscheduler()
35.\" A couple of grammar clean-ups
c11b1abf 36.\" Modified 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
92c37d8c 37.\" 2005-03-23, mtk, Added description of SCHED_BATCH.
cf9c27a6
MK
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.
a1d5601b 41.\" 2008-05-07, mtk; Rewrote and restructured various parts of the page to
a3a22b7f 42.\" improve readability.
fea681da 43.\"
9b6ba4d8 44.\" Worth looking at: http://rt.wiki.kernel.org/index.php
3787794c 45.\"
9b6ba4d8 46.TH SCHED_SETSCHEDULER 2 2008-11-06 "Linux" "Linux Programmer's Manual"
fea681da
MK
47.SH NAME
48sched_setscheduler, sched_getscheduler \-
a3a22b7f 49set and get scheduling policy/parameters
fea681da 50.SH SYNOPSIS
92c37d8c 51.nf
fea681da
MK
52.B #include <sched.h>
53.sp
54.BI "int sched_setscheduler(pid_t " pid ", int " policy ,
92c37d8c
MK
55.br
56.BI " const struct sched_param *" param );
fea681da
MK
57.sp
58.BI "int sched_getscheduler(pid_t " pid );
59.sp
fea681da 60\fBstruct sched_param {
92c37d8c
MK
61 ...
62 int \fIsched_priority\fB;
63 ...
fea681da 64};
fea681da
MK
65.fi
66.SH DESCRIPTION
e511ffb6 67.BR sched_setscheduler ()
fea681da 68sets both the scheduling policy and the associated parameters for the
a3a22b7f 69process whose ID is specified in \fIpid\fP.
c13182ef 70If \fIpid\fP equals zero, the
a3a22b7f 71scheduling policy and parameters of the calling process will be set.
c13182ef 72The interpretation of
c126cfd6 73the argument \fIparam\fP depends on the selected policy.
4e592516
MK
74Currently, Linux supports the following "normal"
75(i.e., non-real-time) scheduling policies:
a3a22b7f 76.TP 14
a1d5601b
MK
77.BR SCHED_OTHER
78the standard round-robin time-sharing policy;
c13182ef 79.\" In the 2.6 kernel sources, SCHED_OTHER is actually called
92c37d8c 80.\" SCHED_NORMAL.
a3a22b7f
MK
81.TP
82.BR SCHED_BATCH
83for "batch" style execution of processes; and
84.TP
85.BR SCHED_IDLE
86for running
87.I very
88low priority background jobs.
89.PP
90The following "real-time" policies are also supported,
91for special time-critical applications that need precise control over
92the way in which runnable processes are selected for execution:
93.TP 14
94.BR SCHED_FIFO
95a first-in, first-out policy; and
96.TP
97.BR SCHED_RR
98a round-robin policy.
99.PP
100The semantics of each of these policies are detailed below.
fea681da 101
e511ffb6 102.BR sched_getscheduler ()
fea681da 103queries the scheduling policy currently applied to the process
c13182ef
MK
104identified by \fIpid\fP.
105If \fIpid\fP equals zero, the policy of the
fea681da 106calling process will be retrieved.
a3a22b7f 107.\"
fea681da 108.SS Scheduling Policies
a3a22b7f 109The scheduler is the kernel component that decides which runnable process
c13182ef 110will be executed by the CPU next.
a3a22b7f
MK
111Each process has an associated scheduling policy and a \fIstatic\fP
112scheduling priority, \fIsched_priority\fP; these are the settings
113that are modified by
114.BR sched_setscheduler ().
115The scheduler makes it decisions based on knowledge of the scheduling
116policy and static priority of all processes on the system.
fea681da 117
a3a22b7f
MK
118For processes 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
121decisions (it must be specified as 0).
92c37d8c 122
a1d5601b 123Processes scheduled under one of the real-time policies
a3a22b7f
MK
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 processes always have higher priority
127than normal processes.)
128Note well: POSIX.1-2001 only requires an implementation to support a
129minimum 32 distinct priority levels for the real-time policies,
6c74a966 130and some systems supply just this minimum.
a3a22b7f 131Portable programs should use
60a90ecd
MK
132.BR sched_get_priority_min (2)
133and
134.BR sched_get_priority_max (2)
a3a22b7f 135to find the range of priorities supported for a particular policy.
fea681da 136
a3a22b7f
MK
137Conceptually, the scheduler maintains a list of runnable
138processes for each possible \fIsched_priority\fP value.
139In order to determine which process runs next, the scheduler looks for
aa796481 140the nonempty list with the highest static priority and selects the
a3a22b7f
MK
141process at the head of this list.
142
143A process's scheduling policy determines
144where it will be inserted into the list of processes
145with equal static priority and how it will move inside this list.
146
147All scheduling is preemptive: if a process with a higher static
148priority becomes ready to run, the currently running process
149will be preempted and
150returned to the wait list for its static priority level.
c13182ef 151The scheduling policy only determines the
fea681da
MK
152ordering within the list of runnable processes with equal static
153priority.
fea681da 154.SS SCHED_FIFO: First In-First Out scheduling
5917ad3d
MK
155\fBSCHED_FIFO\fP can only be used with static priorities higher than
1560, which means that when a \fBSCHED_FIFO\fP processes becomes runnable,
92c37d8c 157it will always immediately preempt any currently running
c00194ab 158\fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP process.
5917ad3d 159\fBSCHED_FIFO\fP is a simple scheduling
c13182ef
MK
160algorithm without time slicing.
161For processes scheduled under the
a3a22b7f
MK
162\fBSCHED_FIFO\fP policy, the following rules apply:
163.IP * 3
164A \fBSCHED_FIFO\fP process that has been preempted by another process of
fea681da
MK
165higher priority will stay at the head of the list for its priority and
166will resume execution as soon as all processes of higher priority are
c13182ef 167blocked again.
a3a22b7f 168.IP *
5917ad3d 169When a \fBSCHED_FIFO\fP process becomes runnable, it
c13182ef 170will be inserted at the end of the list for its priority.
a3a22b7f 171.IP *
c13182ef 172A call to
60a90ecd
MK
173.BR sched_setscheduler ()
174or
175.BR sched_setparam (2)
176will put the
5917ad3d 177\fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) process identified by
fea681da
MK
178\fIpid\fP at the start of the list if it was runnable.
179As a consequence, it may preempt the currently running process if
180it has the same priority.
a7fadb55 181(POSIX.1-2001 specifies that the process should go to the end
fea681da
MK
182of the list.)
183.\" In 2.2.x and 2.4.x, the process is placed at the front of the queue
184.\" In 2.0.x, the Right Thing happened: the process went to the back -- MTK
a3a22b7f 185.IP *
60a90ecd
MK
186A process calling
187.BR sched_yield (2)
a3a22b7f
MK
188will be put at the end of the list.
189.PP
c13182ef 190No other events will move a process
5917ad3d 191scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
c13182ef 192runnable processes with equal static priority.
a3a22b7f 193
5917ad3d 194A \fBSCHED_FIFO\fP
fea681da 195process runs until either it is blocked by an I/O request, it is
60a90ecd
MK
196preempted by a higher priority process, or it calls
197.BR sched_yield (2).
fea681da 198.SS SCHED_RR: Round Robin scheduling
5917ad3d 199\fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
c13182ef 200Everything
5917ad3d 201described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
fea681da 202except that each process is only allowed to run for a maximum time
c13182ef 203quantum.
5917ad3d 204If a \fBSCHED_RR\fP process has been running for a time
fea681da 205period equal to or longer than the time quantum, it will be put at the
c13182ef 206end of the list for its priority.
5917ad3d 207A \fBSCHED_RR\fP process that has
fea681da
MK
208been preempted by a higher priority process and subsequently resumes
209execution as a running process will complete the unexpired portion of
c13182ef
MK
210its round robin time quantum.
211The length of the time quantum can be
60a90ecd
MK
212retrieved using
213.BR sched_rr_get_interval (2).
fea681da
MK
214.\" On Linux 2.4, the length of the RR interval is influenced
215.\" by the process nice value -- MTK
af800319 216.\"
fea681da 217.SS SCHED_OTHER: Default Linux time-sharing scheduling
5917ad3d
MK
218\fBSCHED_OTHER\fP can only be used at static priority 0.
219\fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
a3a22b7f 220intended for all processes that do not require the special
c13182ef
MK
221real-time mechanisms.
222The process to run is chosen from the static
a3a22b7f 223priority 0 list based on a \fIdynamic\fP priority that is determined only
c13182ef 224inside this list.
a3a22b7f 225The dynamic priority is based on the nice value (set by
60a90ecd
MK
226.BR nice (2)
227or
228.BR setpriority (2))
a3a22b7f
MK
229and increased for each time quantum the process is ready to run,
230but denied to run by the scheduler.
231This ensures fair progress among all \fBSCHED_OTHER\fP processes.
232.\"
92c37d8c
MK
233.SS SCHED_BATCH: Scheduling batch processes
234(Since Linux 2.6.16.)
5917ad3d 235\fBSCHED_BATCH\fP can only be used at static priority 0.
6958017f
MK
236This policy is similar to \fBSCHED_OTHER\fP in that it schedules
237the process according to its dynamic priority
238(based on the nice value).
239The difference is that this policy
240will cause the scheduler to always assume
92c37d8c
MK
241that the process is CPU-intensive.
242Consequently, the scheduler will apply a small scheduling
c00194ab
MK
243penalty with respect to wakeup behaviour,
244so that this process is mildly disfavored in scheduling decisions.
a3a22b7f 245
c13182ef
MK
246.\" The following paragraph is drawn largely from the text that
247.\" accompanied Ingo Molnar's patch for the implementation of
92c37d8c
MK
248.\" SCHED_BATCH.
249This policy is useful for workloads that are non-interactive,
c13182ef
MK
250but do not want to lower their nice value,
251and for workloads that want a deterministic scheduling policy without
92c37d8c 252interactivity causing extra preemptions (between the workload's tasks).
c00194ab
MK
253.\"
254.SS SCHED_IDLE: Scheduling very low priority jobs
255(Since Linux 2.6.23.)
256\fBSCHED_IDLE\fP can only be used at static priority 0;
257the process nice value has no influence for this policy.
a3a22b7f 258
c00194ab
MK
259This policy is intended for running jobs at extremely low
260priority (lower even than a +19 nice value with the
261.B SCHED_OTHER
262or
263.B SCHED_BATCH
264policies).
265.\"
fd04afa8 266.SS Privileges and resource limits
fd04afa8
MK
267In Linux kernels before 2.6.12, only privileged
268.RB ( CAP_SYS_NICE )
c7094399 269processes can set a nonzero static priority (i.e., set a real-time
a3a22b7f 270scheduling policy).
afdee10d
MK
271The only change that an unprivileged process can make is to set the
272.B SCHED_OTHER
273policy, and this can only be done if the effective user ID of the caller of
e511ffb6 274.BR sched_setscheduler ()
c13182ef 275matches the real or effective user ID of the target process
afdee10d
MK
276(i.e., the process specified by
277.IR pid )
278whose policy is being changed.
279
fd04afa8
MK
280Since Linux 2.6.12, the
281.B RLIMIT_RTPRIO
afdee10d 282resource limit defines a ceiling on an unprivileged process's
a3a22b7f 283static priority for the
fd04afa8
MK
284.B SCHED_RR
285and
0daa9e92 286.B SCHED_FIFO
fd04afa8 287policies.
4e592516
MK
288The rules for changing scheduling policy and priority are as follows:
289.IP * 2
c7094399 290If an unprivileged process has a nonzero
afdee10d 291.B RLIMIT_RTPRIO
c13182ef
MK
292soft limit, then it can change its scheduling policy and priority,
293subject to the restriction that the priority cannot be set to a
4e592516 294value higher than the maximum of its current priority and its
afdee10d
MK
295.B RLIMIT_RTPRIO
296soft limit.
4e592516 297.IP *
c13182ef 298If the
afdee10d 299.B RLIMIT_RTPRIO
4e592516
MK
300soft limit is 0, then the only permitted changes are to lower the priority,
301or to switch to a non-real-time policy.
302.IP *
c13182ef
MK
303Subject to the same rules,
304another unprivileged process can also make these changes,
305as long as the effective user ID of the process making the change
afdee10d 306matches the real or effective user ID of the target process.
4e592516
MK
307.IP *
308Special rules apply for the
309.BR SCHED_IDLE :
310an unprivileged process operating under this policy cannot
311change its policy, regardless of the value of its
312.BR RLIMIT_RTPRIO
313resource limit.
314.PP
315Privileged
316.RB ( CAP_SYS_NICE )
317processes ignore the
318.B RLIMIT_RTPRIO
319limit; as with older kernels,
320they can make arbitrary changes to scheduling policy and priority.
fd04afa8
MK
321See
322.BR getrlimit (2)
323for further information on
324.BR RLIMIT_RTPRIO .
fea681da
MK
325.SS Response time
326A blocked high priority process waiting for the I/O has a certain
c13182ef
MK
327response time before it is scheduled again.
328The device driver writer
fea681da
MK
329can greatly reduce this response time by using a "slow interrupt"
330interrupt handler.
331.\" as described in
332.\" .BR request_irq (9).
fea681da 333.SS Miscellaneous
a3a22b7f 334Child processes inherit the scheduling policy and parameters across a
0bfa087b 335.BR fork (2).
a3a22b7f 336The scheduling policy and parameters are preserved across
ddb51c37 337.BR execve (2).
fea681da 338
c13182ef 339Memory locking is usually needed for real-time processes to avoid
a3a22b7f 340paging delays; this can be done with
0bfa087b 341.BR mlock (2)
c13182ef 342or
0bfa087b 343.BR mlockall (2).
fea681da 344
ff40dbb3 345Since a nonblocking infinite loop in a process scheduled under
5917ad3d 346\fBSCHED_FIFO\fP or \fBSCHED_RR\fP will block all processes with lower
fea681da
MK
347priority forever, a software developer should always keep available on
348the console a shell scheduled under a higher static priority than the
c13182ef
MK
349tested application.
350This will allow an emergency kill of tested
afdee10d 351real-time applications that do not block or terminate as expected.
a3a22b7f
MK
352See also the description of the
353.BR RLIMIT_RTTIME
354resource limit in
355.BR getrlimit (2).
fea681da
MK
356
357POSIX systems on which
e511ffb6 358.BR sched_setscheduler ()
fea681da 359and
e511ffb6 360.BR sched_getscheduler ()
fea681da 361are available define
f25eaea8 362.B _POSIX_PRIORITY_SCHEDULING
c84371c6 363in \fI<unistd.h>\fP.
fea681da
MK
364.SH "RETURN VALUE"
365On success,
e511ffb6 366.BR sched_setscheduler ()
c13182ef 367returns zero.
fea681da 368On success,
e511ffb6 369.BR sched_getscheduler ()
2fda57bd 370returns the policy for the process (a nonnegative integer).
92c37d8c 371On error, \-1 is returned, and
fea681da
MK
372.I errno
373is set appropriately.
374.SH ERRORS
375.TP
376.B EINVAL
377The scheduling \fIpolicy\fP is not one of the recognized policies,
c126cfd6 378or \fIparam\fP does not make sense for the \fIpolicy\fP.
fea681da
MK
379.TP
380.B EPERM
afdee10d 381The calling process does not have appropriate privileges.
fea681da
MK
382.TP
383.B ESRCH
384The process whose ID is \fIpid\fP could not be found.
385.SH "CONFORMING TO"
d61ff56a 386POSIX.1-2001 (but see BUGS below).
c00194ab 387The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
92c37d8c 388.SH NOTES
97bc0094
MK
389POSIX.1 does not detail the permissions that an unprivileged
390process requires in order to call
f902bebc 391.BR sched_setscheduler (),
97bc0094
MK
392and details vary across systems.
393For example, the Solaris 7 manual page says that
bfd91cc8 394the real or effective user ID of the calling process must
97bc0094 395match the real user ID or the save set-user-ID of the target process.
cf9c27a6
MK
396.PP
397Originally, Standard Linux was intended as a general-purpose operating
398system being able to handle background processes, interactive
399applications, and less demanding real-time applications (applications that
400need to usually meet timing deadlines).
401Although the Linux kernel 2.6
402allowed for kernel preemption and the newly introduced O(1) scheduler
403ensures that the time needed to schedule is fixed and deterministic
404irrespective of the number of active tasks, true real-time computing
405was not possible up to kernel version 2.6.17.
406.SS Real-time features in the mainline Linux kernel
c5871a7d 407.\" FIXME . Probably this text will need some minor tweaking
435ecb8e 408.\" by about the time of 2.6.30; ask Carsten Emde about this then.
cf9c27a6
MK
409From kernel version 2.6.18 onwards, however, Linux is gradually
410becoming equipped with real-time capabilities,
411most of which are derived from the former
83ea1eee 412.I realtime-preempt
435ecb8e
MK
413patches developed by Ingo Molnar, Thomas Gleixner,
414Steven Rostedt, and others.
bf1c0ede 415Until the patches have been completely merged into the
cf9c27a6 416mainline kernel
9b6ba4d8 417(this is expected to be around kernel version 2.6.30),
b77e081d 418they must be installed to achieve the best real-time performance.
cf9c27a6 419These patches are named:
088a639b 420.in +4n
cf9c27a6 421.nf
97bc0094 422
cf9c27a6
MK
423patch-\fIkernelversion\fP-rt\fIpatchversion\fP
424.fi
425.in
fea681da 426.PP
cf9c27a6 427and can be downloaded from
435ecb8e 428.IR http://www.kernel.org/pub/linux/kernel/projects/rt/ .
cf9c27a6
MK
429
430Without the patches and prior to their full inclusion into the mainline
431kernel, the kernel configuration offers only the three preemption classes
432.BR CONFIG_PREEMPT_NONE ,
433.BR CONFIG_PREEMPT_VOLUNTARY ,
434and
0daa9e92 435.B CONFIG_PREEMPT_DESKTOP
cf9c27a6
MK
436which respectively provide no, some, and considerable
437reduction of the worst-case scheduling latency.
438
439With the patches applied or after their full inclusion into the mainline
440kernel, the additional configuration item
0daa9e92 441.B CONFIG_PREEMPT_RT
cf9c27a6
MK
442becomes available.
443If this is selected, Linux is transformed into a regular
444real-time operating system.
445The FIFO and RR scheduling policies that can be selected using
446.BR sched_setscheduler ()
447are then used to run a process
448with true real-time priority and a minimum worst-case scheduling latency.
d61ff56a
MK
449.SH BUGS
450POSIX says that on success,
451.BR sched_setscheduler ()
452should return the previous scheduling policy.
453Linux
454.BR sched_setscheduler ()
455does not conform to this requirement,
456since it always returns 0 on success.
fea681da
MK
457.SH "SEE ALSO"
458.BR getpriority (2),
459.BR mlock (2),
460.BR mlockall (2),
461.BR munlock (2),
462.BR munlockall (2),
463.BR nice (2),
464.BR sched_get_priority_max (2),
465.BR sched_get_priority_min (2),
466.BR sched_getaffinity (2),
467.BR sched_getparam (2),
468.BR sched_rr_get_interval (2),
469.BR sched_setaffinity (2),
470.BR sched_setparam (2),
471.BR sched_yield (2),
472.BR setpriority (2),
a18e2edb
MK
473.BR capabilities (7),
474.BR cpuset (7)
fea681da
MK
475.PP
476.I Programming for the real world \- POSIX.4
477by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0
ea6100ea
MK
478.PP
479The kernel source file
480.I Documentation/scheduler/sched-rt-group.txt
481(since kernel 2.6.25).