]> git.ipfire.org Git - thirdparty/man-pages.git/blame_incremental - man2/sched_setscheduler.2
move_pages.2: Minor tweaks to Yang Shi's patch
[thirdparty/man-pages.git] / man2 / sched_setscheduler.2
... / ...
CommitLineData
1.\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2.\"
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.
7.\"
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.
12.\"
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.
20.\"
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\" %%%LICENSE_END
24.\"
25.\"
26.TH SCHED_SETSCHEDULER 2 2017-09-15 "Linux" "Linux Programmer's Manual"
27.SH NAME
28sched_setscheduler, sched_getscheduler \-
29set and get scheduling policy/parameters
30.SH SYNOPSIS
31.nf
32.B #include <sched.h>
33.PP
34.BI "int sched_setscheduler(pid_t " pid ", int " policy ,
35.BI " const struct sched_param *" param );
36.PP
37.BI "int sched_getscheduler(pid_t " pid );
38.fi
39.SH DESCRIPTION
40The
41.BR sched_setscheduler ()
42system call
43sets both the scheduling policy and parameters for the
44thread whose ID is specified in \fIpid\fP.
45If \fIpid\fP equals zero, the
46scheduling policy and parameters of the calling thread will be set.
47.PP
48The scheduling parameters are specified in the
49.I param
50argument, which is a pointer to a structure of the following form:
51.PP
52.in +4n
53.EX
54struct sched_param {
55 ...
56 int sched_priority;
57 ...
58};
59.EE
60.in
61.PP
62In the current implementation, the structure contains only one field,
63.IR sched_priority .
64The interpretation of
65.I param
66depends on the selected policy.
67.PP
68Currently, Linux supports the following "normal"
69(i.e., non-real-time) scheduling policies as values that may be specified in
70.IR policy :
71.TP 14
72.BR SCHED_OTHER
73the standard round-robin time-sharing policy;
74.\" In the 2.6 kernel sources, SCHED_OTHER is actually called
75.\" SCHED_NORMAL.
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
85For each of the above policies,
86.IR param\->sched_priority
87must be 0.
88.PP
89Various "real-time" policies are also supported,
90for special time-critical applications that need precise control over
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:
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
104For each of the above policies,
105.IR param\->sched_priority
106specifies a scheduling priority for the thread.
107This is a number in the range returned by calling
108.BR sched_get_priority_min (2)
109and
110.BR sched_get_priority_max (2)
111with the specified
112.IR policy .
113On Linux, these system calls return, respectively, 1 and 99.
114.PP
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.
124See
125.BR sched (7)
126for details.
127.PP
128.BR sched_getscheduler ()
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.
133.SH RETURN VALUE
134On success,
135.BR sched_setscheduler ()
136returns zero.
137On success,
138.BR sched_getscheduler ()
139returns the policy for the thread (a nonnegative integer).
140On error, both calls return \-1, and
141.I errno
142is set appropriately.
143.SH ERRORS
144.TP
145.B EINVAL
146Invalid arguments:
147.I pid
148is negative or
149.I param
150is NULL.
151.TP
152.B EINVAL
153.RB ( sched_setscheduler ())
154.I policy
155is not one of the recognized policies.
156.TP
157.B EINVAL
158.RB ( sched_setscheduler ())
159.I param
160does not make sense for the specified
161.IR policy .
162.TP
163.B EPERM
164The calling thread does not have appropriate privileges.
165.TP
166.B ESRCH
167The thread whose ID is \fIpid\fP could not be found.
168.SH CONFORMING TO
169POSIX.1-2001, POSIX.1-2008 (but see BUGS below).
170The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
171.SH NOTES
172Further details of the semantics of all of the above "normal"
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).
180.PP
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.
188.PP
189POSIX.1 does not detail the permissions that an unprivileged
190thread requires in order to call
191.BR sched_setscheduler (),
192and details vary across systems.
193For example, the Solaris 7 manual page says that
194the real or effective user ID of the caller must
195match the real user ID or the save set-user-ID of the target.
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
205as 0 will operate on the attributes of the calling thread,
206and passing the value returned from a call to
207.BR getpid (2)
208will operate on the attributes of the main thread of the thread group.
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.)
217.SH BUGS
218POSIX.1 says that on success,
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.
225.SH SEE ALSO
226.ad l
227.nh
228.BR chrt (1),
229.BR nice (2),
230.BR sched_get_priority_max (2),
231.BR sched_get_priority_min (2),
232.BR sched_getaffinity (2),
233.BR sched_getattr (2),
234.BR sched_getparam (2),
235.BR sched_rr_get_interval (2),
236.BR sched_setaffinity (2),
237.BR sched_setattr (2),
238.BR sched_setparam (2),
239.BR sched_yield (2),
240.BR setpriority (2),
241.BR capabilities (7),
242.BR cpuset (7),
243.BR sched (7)
244.ad