]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/sched_setscheduler.2
sched_setattr.2: tfix
[thirdparty/man-pages.git] / man2 / sched_setscheduler.2
CommitLineData
270d15a2 1.\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 2.\"
270d15a2
MK
3.\" %%%LICENSE_START(VERBATIM)
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
fea681da 7.\"
270d15a2
MK
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
fea681da 12.\"
270d15a2
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
fea681da 20.\"
270d15a2
MK
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
6a8d8745 23.\" %%%LICENSE_END
fea681da 24.\"
fea681da 25.\"
4b8c67d9 26.TH SCHED_SETSCHEDULER 2 2017-09-15 "Linux" "Linux Programmer's Manual"
fea681da
MK
27.SH NAME
28sched_setscheduler, sched_getscheduler \-
a3a22b7f 29set and get scheduling policy/parameters
fea681da 30.SH SYNOPSIS
92c37d8c 31.nf
fea681da 32.B #include <sched.h>
68e4db0a 33.PP
fea681da 34.BI "int sched_setscheduler(pid_t " pid ", int " policy ,
92c37d8c 35.BI " const struct sched_param *" param );
68e4db0a 36.PP
fea681da 37.BI "int sched_getscheduler(pid_t " pid );
fea681da
MK
38.fi
39.SH DESCRIPTION
270d15a2 40The
e511ffb6 41.BR sched_setscheduler ()
270d15a2
MK
42system call
43sets both the scheduling policy and parameters for the
81bd66da 44thread whose ID is specified in \fIpid\fP.
c13182ef 45If \fIpid\fP equals zero, the
81bd66da 46scheduling policy and parameters of the calling thread will be set.
efeece04 47.PP
270d15a2
MK
48The scheduling parameters are specified in the
49.I param
b9136726 50argument, which is a pointer to a structure of the following form:
efeece04 51.PP
270d15a2 52.in +4n
b8302363 53.EX
270d15a2
MK
54struct sched_param {
55 ...
56 int sched_priority;
57 ...
58};
b8302363 59.EE
e646a1ba 60.in
efeece04 61.PP
270d15a2
MK
62In the current implementation, the structure contains only one field,
63.IR sched_priority .
c13182ef 64The interpretation of
270d15a2
MK
65.I param
66depends on the selected policy.
efeece04 67.PP
4e592516 68Currently, Linux supports the following "normal"
270d15a2
MK
69(i.e., non-real-time) scheduling policies as values that may be specified in
70.IR policy :
a3a22b7f 71.TP 14
a1d5601b
MK
72.BR SCHED_OTHER
73the standard round-robin time-sharing policy;
c13182ef 74.\" In the 2.6 kernel sources, SCHED_OTHER is actually called
92c37d8c 75.\" SCHED_NORMAL.
a3a22b7f
MK
76.TP
77.BR SCHED_BATCH
78for "batch" style execution of processes; and
79.TP
80.BR SCHED_IDLE
81for running
82.I very
83low priority background jobs.
84.PP
270d15a2
MK
85For each of the above policies,
86.IR param\->sched_priority
87must be 0.
efeece04 88.PP
270d15a2 89Various "real-time" policies are also supported,
a3a22b7f 90for special time-critical applications that need precise control over
270d15a2
MK
91the way in which runnable threads are selected for execution.
92For the rules governing when a process may use these policies, see
93.BR sched (7).
94The real-time policies that may be specified in
95.IR policy
96are:
a3a22b7f
MK
97.TP 14
98.BR SCHED_FIFO
99a first-in, first-out policy; and
100.TP
101.BR SCHED_RR
102a round-robin policy.
103.PP
270d15a2
MK
104For each of the above policies,
105.IR param\->sched_priority
106specifies a scheduling priority for the thread.
ab3d0245 107This is a number in the range returned by calling
60a90ecd
MK
108.BR sched_get_priority_min (2)
109and
59ab8fcf 110.BR sched_get_priority_max (2)
270d15a2
MK
111with the specified
112.IR policy .
113On Linux, these system calls return, respectively, 1 and 99.
efeece04 114.PP
93414d35
MK
115Since Linux 2.6.32, the
116.B SCHED_RESET_ON_FORK
117flag can be ORed in
118.I policy
119when calling
120.BR sched_setscheduler ().
121As a result of including this flag, children created by
122.BR fork (2)
123do not inherit privileged scheduling policies.
fd04afa8 124See
270d15a2
MK
125.BR sched (7)
126for details.
efeece04 127.PP
e511ffb6 128.BR sched_getscheduler ()
270d15a2
MK
129returns the current scheduling policy of the thread
130identified by \fIpid\fP.
131If \fIpid\fP equals zero, the policy of the
132calling thread will be retrieved.
47297adb 133.SH RETURN VALUE
fea681da 134On success,
e511ffb6 135.BR sched_setscheduler ()
c13182ef 136returns zero.
fea681da 137On success,
e511ffb6 138.BR sched_getscheduler ()
81bd66da 139returns the policy for the thread (a nonnegative integer).
270d15a2 140On error, both calls return \-1, and
fea681da
MK
141.I errno
142is set appropriately.
143.SH ERRORS
144.TP
145.B EINVAL
b61826c0 146Invalid arguments:
94417494 147.I pid
b61826c0
MK
148is negative or
149.I param
150is NULL.
151.TP
152.B EINVAL
6a6dd344 153.RB ( sched_setscheduler ())
b61826c0
MK
154.I policy
155is not one of the recognized policies.
156.TP
157.B EINVAL
6a6dd344 158.RB ( sched_setscheduler ())
b61826c0
MK
159.I param
160does not make sense for the specified
161.IR policy .
fea681da
MK
162.TP
163.B EPERM
81bd66da 164The calling thread does not have appropriate privileges.
fea681da
MK
165.TP
166.B ESRCH
81bd66da 167The thread whose ID is \fIpid\fP could not be found.
47297adb 168.SH CONFORMING TO
428a4469 169POSIX.1-2001, POSIX.1-2008 (but see BUGS below).
c00194ab 170The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
92c37d8c 171.SH NOTES
270d15a2 172Further details of the semantics of all of the above "normal"
16109ab3
MK
173and "real-time" scheduling policies can be found in the
174.BR sched (7)
175manual page.
176That page also describes an additional policy,
177.BR SCHED_DEADLINE ,
178which is settable only via
179.BR sched_setattr (2).
efeece04 180.PP
270d15a2
MK
181POSIX systems on which
182.BR sched_setscheduler ()
183and
184.BR sched_getscheduler ()
185are available define
186.B _POSIX_PRIORITY_SCHEDULING
187in \fI<unistd.h>\fP.
efeece04 188.PP
97bc0094 189POSIX.1 does not detail the permissions that an unprivileged
81bd66da 190thread requires in order to call
f902bebc 191.BR sched_setscheduler (),
97bc0094
MK
192and details vary across systems.
193For example, the Solaris 7 manual page says that
81bd66da
MK
194the real or effective user ID of the caller must
195match the real user ID or the save set-user-ID of the target.
d5999511
MK
196.PP
197The scheduling policy and parameters are in fact per-thread
198attributes on Linux.
199The value returned from a call to
200.BR gettid (2)
201can be passed in the argument
202.IR pid .
203Specifying
204.I pid
ab3d0245 205as 0 will operate on the attributes of the calling thread,
d5999511
MK
206and passing the value returned from a call to
207.BR getpid (2)
ab3d0245 208will operate on the attributes of the main thread of the thread group.
d5999511
MK
209(If you are using the POSIX threads API, then use
210.BR pthread_setschedparam (3),
211.BR pthread_getschedparam (3),
212and
213.BR pthread_setschedprio (3),
214instead of the
215.BR sched_* (2)
216system calls.)
d61ff56a 217.SH BUGS
99940e05 218POSIX.1 says that on success,
d61ff56a
MK
219.BR sched_setscheduler ()
220should return the previous scheduling policy.
221Linux
222.BR sched_setscheduler ()
223does not conform to this requirement,
224since it always returns 0 on success.
47297adb 225.SH SEE ALSO
ca8a0bd2
MK
226.ad l
227.nh
ac30de07 228.BR chrt (1),
fea681da
MK
229.BR nice (2),
230.BR sched_get_priority_max (2),
231.BR sched_get_priority_min (2),
232.BR sched_getaffinity (2),
270d15a2 233.BR sched_getattr (2),
fea681da
MK
234.BR sched_getparam (2),
235.BR sched_rr_get_interval (2),
236.BR sched_setaffinity (2),
270d15a2 237.BR sched_setattr (2),
fea681da
MK
238.BR sched_setparam (2),
239.BR sched_yield (2),
240.BR setpriority (2),
a18e2edb 241.BR capabilities (7),
270d15a2
MK
242.BR cpuset (7),
243.BR sched (7)
ab13a138 244.ad