]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/sched_setscheduler.2
ldd.1, sprof.1, accept.2, alarm.2, bind.2, chdir.2, clock_nanosleep.2, close.2, conne...
[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.\"
460495ca 26.TH SCHED_SETSCHEDULER 2 2015-08-08 "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
MK
32.B #include <sched.h>
33.sp
34.BI "int sched_setscheduler(pid_t " pid ", int " policy ,
92c37d8c
MK
35.br
36.BI " const struct sched_param *" param );
fea681da
MK
37.sp
38.BI "int sched_getscheduler(pid_t " pid );
39.sp
fea681da
MK
40.fi
41.SH DESCRIPTION
270d15a2 42The
e511ffb6 43.BR sched_setscheduler ()
270d15a2
MK
44system call
45sets both the scheduling policy and parameters for the
81bd66da 46thread whose ID is specified in \fIpid\fP.
c13182ef 47If \fIpid\fP equals zero, the
81bd66da 48scheduling policy and parameters of the calling thread will be set.
270d15a2
MK
49
50The scheduling parameters are specified in the
51.I param
b9136726 52argument, which is a pointer to a structure of the following form:
270d15a2
MK
53
54.nf
55.in +4n
56struct sched_param {
57 ...
58 int sched_priority;
59 ...
60};
61.in
62.fi
63
64In the current implementation, the structure contains only one field,
65.IR sched_priority .
c13182ef 66The interpretation of
270d15a2
MK
67.I param
68depends on the selected policy.
69
4e592516 70Currently, Linux supports the following "normal"
270d15a2
MK
71(i.e., non-real-time) scheduling policies as values that may be specified in
72.IR policy :
a3a22b7f 73.TP 14
a1d5601b
MK
74.BR SCHED_OTHER
75the standard round-robin time-sharing policy;
c13182ef 76.\" In the 2.6 kernel sources, SCHED_OTHER is actually called
92c37d8c 77.\" SCHED_NORMAL.
a3a22b7f
MK
78.TP
79.BR SCHED_BATCH
80for "batch" style execution of processes; and
81.TP
82.BR SCHED_IDLE
83for running
84.I very
85low priority background jobs.
86.PP
270d15a2
MK
87For each of the above policies,
88.IR param\->sched_priority
89must be 0.
90
91Various "real-time" policies are also supported,
a3a22b7f 92for special time-critical applications that need precise control over
270d15a2
MK
93the way in which runnable threads are selected for execution.
94For the rules governing when a process may use these policies, see
95.BR sched (7).
96The real-time policies that may be specified in
97.IR policy
98are:
a3a22b7f
MK
99.TP 14
100.BR SCHED_FIFO
101a first-in, first-out policy; and
102.TP
103.BR SCHED_RR
104a round-robin policy.
105.PP
270d15a2
MK
106For each of the above policies,
107.IR param\->sched_priority
108specifies a scheduling priority for the thread.
ab3d0245 109This is a number in the range returned by calling
60a90ecd
MK
110.BR sched_get_priority_min (2)
111and
59ab8fcf 112.BR sched_get_priority_max (2)
270d15a2
MK
113with the specified
114.IR policy .
115On Linux, these system calls return, respectively, 1 and 99.
a3a22b7f 116
93414d35
MK
117Since Linux 2.6.32, the
118.B SCHED_RESET_ON_FORK
119flag can be ORed in
120.I policy
121when calling
122.BR sched_setscheduler ().
123As a result of including this flag, children created by
124.BR fork (2)
125do not inherit privileged scheduling policies.
fd04afa8 126See
270d15a2
MK
127.BR sched (7)
128for details.
fea681da 129
e511ffb6 130.BR sched_getscheduler ()
270d15a2
MK
131returns the current scheduling policy of the thread
132identified by \fIpid\fP.
133If \fIpid\fP equals zero, the policy of the
134calling thread will be retrieved.
47297adb 135.SH RETURN VALUE
fea681da 136On success,
e511ffb6 137.BR sched_setscheduler ()
c13182ef 138returns zero.
fea681da 139On success,
e511ffb6 140.BR sched_getscheduler ()
81bd66da 141returns the policy for the thread (a nonnegative integer).
270d15a2 142On error, both calls return \-1, and
fea681da
MK
143.I errno
144is set appropriately.
145.SH ERRORS
146.TP
147.B EINVAL
b61826c0 148Invalid arguments:
94417494 149.I pid
b61826c0
MK
150is negative or
151.I param
152is NULL.
153.TP
154.B EINVAL
6a6dd344 155.RB ( sched_setscheduler ())
b61826c0
MK
156.I policy
157is not one of the recognized policies.
158.TP
159.B EINVAL
6a6dd344 160.RB ( sched_setscheduler ())
b61826c0
MK
161.I param
162does not make sense for the specified
163.IR policy .
fea681da
MK
164.TP
165.B EPERM
81bd66da 166The calling thread does not have appropriate privileges.
fea681da
MK
167.TP
168.B ESRCH
81bd66da 169The thread whose ID is \fIpid\fP could not be found.
47297adb 170.SH CONFORMING TO
428a4469 171POSIX.1-2001, POSIX.1-2008 (but see BUGS below).
c00194ab 172The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
92c37d8c 173.SH NOTES
270d15a2 174Further details of the semantics of all of the above "normal"
3d1ee497 175and "real-time" scheduling policies can be found in
270d15a2
MK
176.BR sched (7).
177
178POSIX systems on which
179.BR sched_setscheduler ()
180and
181.BR sched_getscheduler ()
182are available define
183.B _POSIX_PRIORITY_SCHEDULING
184in \fI<unistd.h>\fP.
185
97bc0094 186POSIX.1 does not detail the permissions that an unprivileged
81bd66da 187thread requires in order to call
f902bebc 188.BR sched_setscheduler (),
97bc0094
MK
189and details vary across systems.
190For example, the Solaris 7 manual page says that
81bd66da
MK
191the real or effective user ID of the caller must
192match the real user ID or the save set-user-ID of the target.
d5999511
MK
193.PP
194The scheduling policy and parameters are in fact per-thread
195attributes on Linux.
196The value returned from a call to
197.BR gettid (2)
198can be passed in the argument
199.IR pid .
200Specifying
201.I pid
ab3d0245 202as 0 will operate on the attributes of the calling thread,
d5999511
MK
203and passing the value returned from a call to
204.BR getpid (2)
ab3d0245 205will operate on the attributes of the main thread of the thread group.
d5999511
MK
206(If you are using the POSIX threads API, then use
207.BR pthread_setschedparam (3),
208.BR pthread_getschedparam (3),
209and
210.BR pthread_setschedprio (3),
211instead of the
212.BR sched_* (2)
213system calls.)
d61ff56a 214.SH BUGS
99940e05 215POSIX.1 says that on success,
d61ff56a
MK
216.BR sched_setscheduler ()
217should return the previous scheduling policy.
218Linux
219.BR sched_setscheduler ()
220does not conform to this requirement,
221since it always returns 0 on success.
47297adb 222.SH SEE ALSO
ca8a0bd2
MK
223.ad l
224.nh
ac30de07 225.BR chrt (1),
fea681da
MK
226.BR nice (2),
227.BR sched_get_priority_max (2),
228.BR sched_get_priority_min (2),
229.BR sched_getaffinity (2),
270d15a2 230.BR sched_getattr (2),
fea681da
MK
231.BR sched_getparam (2),
232.BR sched_rr_get_interval (2),
233.BR sched_setaffinity (2),
270d15a2 234.BR sched_setattr (2),
fea681da
MK
235.BR sched_setparam (2),
236.BR sched_yield (2),
237.BR setpriority (2),
a18e2edb 238.BR capabilities (7),
270d15a2
MK
239.BR cpuset (7),
240.BR sched (7)
ab13a138 241.ad