1SCHED_SETSCHEDULER(2) Linux Programmer's Manual SCHED_SETSCHEDULER(2)
2
3
4
6 sched_setscheduler, sched_getscheduler - set and get scheduling pol‐
7 icy/parameters
8
10 #include <sched.h>
11
12 int sched_setscheduler(pid_t pid, int policy,
13 const struct sched_param *param);
14
15 int sched_getscheduler(pid_t pid);
16
17 struct sched_param {
18 ...
19 int sched_priority;
20 ...
21 };
22
24 sched_setscheduler() sets both the scheduling policy and the associated
25 parameters for the process whose ID is specified in pid. If pid equals
26 zero, the scheduling policy and parameters of the calling process will
27 be set. The interpretation of the argument param depends on the
28 selected policy. Currently, Linux supports the following "normal"
29 (i.e., non-real-time) scheduling policies:
30
31 SCHED_OTHER the standard round-robin time-sharing policy;
32
33 SCHED_BATCH for "batch" style execution of processes; and
34
35 SCHED_IDLE for running very low priority background jobs.
36
37 The following "real-time" policies are also supported, for special
38 time-critical applications that need precise control over the way in
39 which runnable processes are selected for execution:
40
41 SCHED_FIFO a first-in, first-out policy; and
42
43 SCHED_RR a round-robin policy.
44
45 The semantics of each of these policies are detailed below.
46
47 sched_getscheduler() queries the scheduling policy currently applied to
48 the process identified by pid. If pid equals zero, the policy of the
49 calling process will be retrieved.
50
51 Scheduling policies
52 The scheduler is the kernel component that decides which runnable
53 process will be executed by the CPU next. Each process has an associ‐
54 ated scheduling policy and a static scheduling priority, sched_prior‐
55 ity; these are the settings that are modified by sched_setscheduler().
56 The scheduler makes it decisions based on knowledge of the scheduling
57 policy and static priority of all processes on the system.
58
59 For processes scheduled under one of the normal scheduling policies
60 (SCHED_OTHER, SCHED_IDLE, SCHED_BATCH), sched_priority is not used in
61 scheduling decisions (it must be specified as 0).
62
63 Processes scheduled under one of the real-time policies (SCHED_FIFO,
64 SCHED_RR) have a sched_priority value in the range 1 (low) to 99
65 (high). (As the numbers imply, real-time processes always have higher
66 priority than normal processes.) Note well: POSIX.1-2001 requires an
67 implementation to support only a minimum 32 distinct priority levels
68 for the real-time policies, and some systems supply just this minimum.
69 Portable programs should use sched_get_priority_min(2) and
70 sched_get_priority_max(2) to find the range of priorities supported for
71 a particular policy.
72
73 Conceptually, the scheduler maintains a list of runnable processes for
74 each possible sched_priority value. In order to determine which
75 process runs next, the scheduler looks for the nonempty list with the
76 highest static priority and selects the process at the head of this
77 list.
78
79 A process's scheduling policy determines where it will be inserted into
80 the list of processes with equal static priority and how it will move
81 inside this list.
82
83 All scheduling is preemptive: if a process with a higher static prior‐
84 ity becomes ready to run, the currently running process will be pre‐
85 empted and returned to the wait list for its static priority level.
86 The scheduling policy determines the ordering only within the list of
87 runnable processes with equal static priority.
88
89 SCHED_FIFO: First in-first out scheduling
90 SCHED_FIFO can be used only with static priorities higher than 0, which
91 means that when a SCHED_FIFO processes becomes runnable, it will always
92 immediately preempt any currently running SCHED_OTHER, SCHED_BATCH, or
93 SCHED_IDLE process. SCHED_FIFO is a simple scheduling algorithm with‐
94 out time slicing. For processes scheduled under the SCHED_FIFO policy,
95 the following rules apply:
96
97 * A SCHED_FIFO process that has been preempted by another process of
98 higher priority will stay at the head of the list for its priority
99 and will resume execution as soon as all processes of higher prior‐
100 ity are blocked again.
101
102 * When a SCHED_FIFO process becomes runnable, it will be inserted at
103 the end of the list for its priority.
104
105 * A call to sched_setscheduler() or sched_setparam(2) will put the
106 SCHED_FIFO (or SCHED_RR) process identified by pid at the start of
107 the list if it was runnable. As a consequence, it may preempt the
108 currently running process if it has the same priority.
109 (POSIX.1-2001 specifies that the process should go to the end of the
110 list.)
111
112 * A process calling sched_yield(2) will be put at the end of the list.
113
114 No other events will move a process scheduled under the SCHED_FIFO pol‐
115 icy in the wait list of runnable processes with equal static priority.
116
117 A SCHED_FIFO process runs until either it is blocked by an I/O request,
118 it is preempted by a higher priority process, or it calls
119 sched_yield(2).
120
121 SCHED_RR: Round-robin scheduling
122 SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described
123 above for SCHED_FIFO also applies to SCHED_RR, except that each process
124 is allowed to run only for a maximum time quantum. If a SCHED_RR
125 process has been running for a time period equal to or longer than the
126 time quantum, it will be put at the end of the list for its priority.
127 A SCHED_RR process that has been preempted by a higher priority process
128 and subsequently resumes execution as a running process will complete
129 the unexpired portion of its round-robin time quantum. The length of
130 the time quantum can be retrieved using sched_rr_get_interval(2).
131
132 SCHED_OTHER: Default Linux time-sharing scheduling
133 SCHED_OTHER can be used at only static priority 0. SCHED_OTHER is the
134 standard Linux time-sharing scheduler that is intended for all pro‐
135 cesses that do not require the special real-time mechanisms. The
136 process to run is chosen from the static priority 0 list based on a
137 dynamic priority that is determined only inside this list. The dynamic
138 priority is based on the nice value (set by nice(2) or setpriority(2))
139 and increased for each time quantum the process is ready to run, but
140 denied to run by the scheduler. This ensures fair progress among all
141 SCHED_OTHER processes.
142
143 SCHED_BATCH: Scheduling batch processes
144 (Since Linux 2.6.16.) SCHED_BATCH can be used only at static priority
145 0. This policy is similar to SCHED_OTHER in that it schedules the
146 process according to its dynamic priority (based on the nice value).
147 The difference is that this policy will cause the scheduler to always
148 assume that the process is CPU-intensive. Consequently, the scheduler
149 will apply a small scheduling penalty with respect to wakeup behaviour,
150 so that this process is mildly disfavored in scheduling decisions.
151
152 This policy is useful for workloads that are noninteractive, but do not
153 want to lower their nice value, and for workloads that want a determin‐
154 istic scheduling policy without interactivity causing extra preemptions
155 (between the workload's tasks).
156
157 SCHED_IDLE: Scheduling very low priority jobs
158 (Since Linux 2.6.23.) SCHED_IDLE can be used only at static priority
159 0; the process nice value has no influence for this policy.
160
161 This policy is intended for running jobs at extremely low priority
162 (lower even than a +19 nice value with the SCHED_OTHER or SCHED_BATCH
163 policies).
164
165 Resetting scheduling policy for child processes
166 Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag can be ORed in policy
167 when calling sched_setscheduler(). As a result of including this flag,
168 children created by fork(2) do not inherit privileged scheduling poli‐
169 cies. This feature is intended for media-playback applications, and
170 can be used to prevent applications evading the RLIMIT_RTTIME resource
171 limit (see getrlimit(2)) by creating multiple child processes.
172
173 More precisely, if the SCHED_RESET_ON_FORK flag is specified, the fol‐
174 lowing rules apply for subsequently created children:
175
176 * If the calling process has a scheduling policy of SCHED_FIFO or
177 SCHED_RR, the policy is reset to SCHED_OTHER in child processes.
178
179 * If the calling process has a negative nice value, the nice value is
180 reset to zero in child processes.
181
182 After the SCHED_RESET_ON_FORK flag has been enabled, it can be reset
183 only if the process has the CAP_SYS_NICE capability. This flag is dis‐
184 abled in child processes created by fork(2).
185
186 The SCHED_RESET_ON_FORK flag is visible in the policy value returned by
187 sched_getscheduler()
188
189 Privileges and resource limits
190 In Linux kernels before 2.6.12, only privileged (CAP_SYS_NICE) pro‐
191 cesses can set a nonzero static priority (i.e., set a real-time sched‐
192 uling policy). The only change that an unprivileged process can make
193 is to set the SCHED_OTHER policy, and this can be done only if the
194 effective user ID of the caller of sched_setscheduler() matches the
195 real or effective user ID of the target process (i.e., the process
196 specified by pid) whose policy is being changed.
197
198 Since Linux 2.6.12, the RLIMIT_RTPRIO resource limit defines a ceiling
199 on an unprivileged process's static priority for the SCHED_RR and
200 SCHED_FIFO policies. The rules for changing scheduling policy and pri‐
201 ority are as follows:
202
203 * If an unprivileged process has a nonzero RLIMIT_RTPRIO soft limit,
204 then it can change its scheduling policy and priority, subject to
205 the restriction that the priority cannot be set to a value higher
206 than the maximum of its current priority and its RLIMIT_RTPRIO soft
207 limit.
208
209 * If the RLIMIT_RTPRIO soft limit is 0, then the only permitted
210 changes are to lower the priority, or to switch to a non-real-time
211 policy.
212
213 * Subject to the same rules, another unprivileged process can also
214 make these changes, as long as the effective user ID of the process
215 making the change matches the real or effective user ID of the tar‐
216 get process.
217
218 * Special rules apply for the SCHED_IDLE. In Linux kernels before
219 2.6.39, an unprivileged process operating under this policy cannot
220 change its policy, regardless of the value of its RLIMIT_RTPRIO
221 resource limit. In Linux kernels since 2.6.39, an unprivileged
222 process can switch to either the SCHED_BATCH or the SCHED_NORMAL
223 policy so long as its nice value falls within the range permitted by
224 its RLIMIT_NICE resource limit (see getrlimit(2)).
225
226 Privileged (CAP_SYS_NICE) processes ignore the RLIMIT_RTPRIO limit; as
227 with older kernels, they can make arbitrary changes to scheduling pol‐
228 icy and priority. See getrlimit(2) for further information on
229 RLIMIT_RTPRIO.
230
231 Response time
232 A blocked high priority process waiting for the I/O has a certain
233 response time before it is scheduled again. The device driver writer
234 can greatly reduce this response time by using a "slow interrupt"
235 interrupt handler.
236
237 Miscellaneous
238 Child processes inherit the scheduling policy and parameters across a
239 fork(2). The scheduling policy and parameters are preserved across
240 execve(2).
241
242 Memory locking is usually needed for real-time processes to avoid pag‐
243 ing delays; this can be done with mlock(2) or mlockall(2).
244
245 Since a nonblocking infinite loop in a process scheduled under
246 SCHED_FIFO or SCHED_RR will block all processes with lower priority
247 forever, a software developer should always keep available on the con‐
248 sole a shell scheduled under a higher static priority than the tested
249 application. This will allow an emergency kill of tested real-time
250 applications that do not block or terminate as expected. See also the
251 description of the RLIMIT_RTTIME resource limit in getrlimit(2).
252
253 POSIX systems on which sched_setscheduler() and sched_getscheduler()
254 are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.
255
257 On success, sched_setscheduler() returns zero. On success,
258 sched_getscheduler() returns the policy for the process (a nonnegative
259 integer). On error, -1 is returned, and errno is set appropriately.
260
262 EINVAL The scheduling policy is not one of the recognized policies,
263 param is NULL, or param does not make sense for the policy.
264
265 EPERM The calling process does not have appropriate privileges.
266
267 ESRCH The process whose ID is pid could not be found.
268
270 POSIX.1-2001 (but see BUGS below). The SCHED_BATCH and SCHED_IDLE
271 policies are Linux-specific.
272
274 POSIX.1 does not detail the permissions that an unprivileged process
275 requires in order to call sched_setscheduler(), and details vary across
276 systems. For example, the Solaris 7 manual page says that the real or
277 effective user ID of the calling process must match the real user ID or
278 the save set-user-ID of the target process.
279
280 The scheduling policy and parameters are in fact per-thread attributes
281 on Linux. The value returned from a call to gettid(2) can be passed in
282 the argument pid. Specifying pid as 0 will operate on the attribute
283 for the calling thread, and passing the value returned from a call to
284 getpid(2) will operate on the attribute for the main thread of the
285 thread group. (If you are using the POSIX threads API, then use
286 pthread_setschedparam(3), pthread_getschedparam(3), and
287 pthread_setschedprio(3), instead of the sched_*[22m(2) system calls.)
288
289 Originally, Standard Linux was intended as a general-purpose operating
290 system being able to handle background processes, interactive applica‐
291 tions, and less demanding real-time applications (applications that
292 need to usually meet timing deadlines). Although the Linux kernel 2.6
293 allowed for kernel preemption and the newly introduced O(1) scheduler
294 ensures that the time needed to schedule is fixed and deterministic
295 irrespective of the number of active tasks, true real-time computing
296 was not possible up to kernel version 2.6.17.
297
298 Real-time features in the mainline Linux kernel
299 From kernel version 2.6.18 onward, however, Linux is gradually becoming
300 equipped with real-time capabilities, most of which are derived from
301 the former realtime-preempt patches developed by Ingo Molnar, Thomas
302 Gleixner, Steven Rostedt, and others. Until the patches have been com‐
303 pletely merged into the mainline kernel (this is expected to be around
304 kernel version 2.6.30), they must be installed to achieve the best
305 real-time performance. These patches are named:
306
307 patch-kernelversion-rtpatchversion
308
309 and can be downloaded from ⟨http://www.kernel.org/pub/linux/kernel
310 /projects/rt/⟩.
311
312 Without the patches and prior to their full inclusion into the mainline
313 kernel, the kernel configuration offers only the three preemption
314 classes CONFIG_PREEMPT_NONE, CONFIG_PREEMPT_VOLUNTARY, and CONFIG_PRE‐
315 EMPT_DESKTOP which respectively provide no, some, and considerable
316 reduction of the worst-case scheduling latency.
317
318 With the patches applied or after their full inclusion into the main‐
319 line kernel, the additional configuration item CONFIG_PREEMPT_RT
320 becomes available. If this is selected, Linux is transformed into a
321 regular real-time operating system. The FIFO and RR scheduling poli‐
322 cies that can be selected using sched_setscheduler() are then used to
323 run a process with true real-time priority and a minimum worst-case
324 scheduling latency.
325
327 POSIX says that on success, sched_setscheduler() should return the pre‐
328 vious scheduling policy. Linux sched_setscheduler() does not conform
329 to this requirement, since it always returns 0 on success.
330
332 chrt(1), getpriority(2), mlock(2), mlockall(2), munlock(2),
333 munlockall(2), nice(2), sched_get_priority_max(2),
334 sched_get_priority_min(2), sched_getaffinity(2), sched_getparam(2),
335 sched_rr_get_interval(2), sched_setaffinity(2), sched_setparam(2),
336 sched_yield(2), setpriority(2), capabilities(7), cpuset(7)
337
338 Programming for the real world - POSIX.4 by Bill O. Gallmeister,
339 O'Reilly & Associates, Inc., ISBN 1-56592-074-0.
340
341 The Linux kernel source file Documentation/scheduler/sched-rt-group.txt
342
344 This page is part of release 3.53 of the Linux man-pages project. A
345 description of the project, and information about reporting bugs, can
346 be found at http://www.kernel.org/doc/man-pages/.
347
348
349
350Linux 2013-02-12 SCHED_SETSCHEDULER(2)