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