OSDN Git Service

96ccfe6a40d5c554183dcacc5600a97fcc7641ea
[linuxjm/LDP_man-pages.git] / original / man2 / sched_setscheduler.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright (C) Tom Bjorkholm, Markus Kuhn & David A. Wheeler 1996-1999
4 .\" and Copyright (C) 2007 Carsten Emde <Carsten.Emde@osadl.org>
5 .\" and Copyright (C) 2008 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\"
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
11 .\"
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
16 .\"
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 .\" GNU General Public License for more details.
21 .\"
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, write to the Free
24 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
25 .\" USA.
26 .\"
27 .\" 1996-04-01 Tom Bjorkholm <tomb@mydata.se>
28 .\"            First version written
29 .\" 1996-04-10 Markus Kuhn <mskuhn@cip.informatik.uni-erlangen.de>
30 .\"            revision
31 .\" 1999-08-18 David A. Wheeler <dwheeler@ida.org> added Note.
32 .\" Modified, 25 Jun 2002, Michael Kerrisk <mtk.manpages@gmail.com>
33 .\"     Corrected description of queue placement by sched_setparam() and
34 .\"             sched_setscheduler()
35 .\"     A couple of grammar clean-ups
36 .\" Modified 2004-05-27 by Michael Kerrisk <mtk.manpages@gmail.com>
37 .\" 2005-03-23, mtk, Added description of SCHED_BATCH.
38 .\" 2007-07-10, Carsten Emde <Carsten.Emde@osadl.org>
39 .\"     Add text on real-time features that are currently being
40 .\"     added to the mainline kernel.
41 .\" 2008-05-07, mtk; Rewrote and restructured various parts of the page to
42 .\"     improve readability.
43 .\" 2010-06-19, mtk, documented SCHED_RESET_ON_FORK
44 .\"
45 .\" Worth looking at: http://rt.wiki.kernel.org/index.php
46 .\"
47 .TH SCHED_SETSCHEDULER 2 2010-10-04 "Linux" "Linux Programmer's Manual"
48 .SH NAME
49 sched_setscheduler, sched_getscheduler \-
50 set and get scheduling policy/parameters
51 .SH SYNOPSIS
52 .nf
53 .B #include <sched.h>
54 .sp
55 .BI "int sched_setscheduler(pid_t " pid ", int " policy ,
56 .br
57 .BI "                       const struct sched_param *" param );
58 .sp
59 .BI "int sched_getscheduler(pid_t " pid );
60 .sp
61 \fBstruct sched_param {
62     ...
63     int \fIsched_priority\fB;
64     ...
65 };
66 .fi
67 .SH DESCRIPTION
68 .BR sched_setscheduler ()
69 sets both the scheduling policy and the associated parameters for the
70 process whose ID is specified in \fIpid\fP.
71 If \fIpid\fP equals zero, the
72 scheduling policy and parameters of the calling process will be set.
73 The interpretation of
74 the argument \fIparam\fP depends on the selected policy.
75 Currently, Linux supports the following "normal"
76 (i.e., non-real-time) scheduling policies:
77 .TP 14
78 .BR SCHED_OTHER
79 the standard round-robin time-sharing policy;
80 .\" In the 2.6 kernel sources, SCHED_OTHER is actually called
81 .\" SCHED_NORMAL.
82 .TP
83 .BR SCHED_BATCH
84 for "batch" style execution of processes; and
85 .TP
86 .BR SCHED_IDLE
87 for running
88 .I very
89 low priority background jobs.
90 .PP
91 The following "real-time" policies are also supported,
92 for special time-critical applications that need precise control over
93 the way in which runnable processes are selected for execution:
94 .TP 14
95 .BR SCHED_FIFO
96 a first-in, first-out policy; and
97 .TP
98 .BR SCHED_RR
99 a round-robin policy.
100 .PP
101 The semantics of each of these policies are detailed below.
102
103 .BR sched_getscheduler ()
104 queries the scheduling policy currently applied to the process
105 identified by \fIpid\fP.
106 If \fIpid\fP equals zero, the policy of the
107 calling process will be retrieved.
108 .\"
109 .SS Scheduling Policies
110 The scheduler is the kernel component that decides which runnable process
111 will be executed by the CPU next.
112 Each process has an associated scheduling policy and a \fIstatic\fP
113 scheduling priority, \fIsched_priority\fP; these are the settings
114 that are modified by
115 .BR sched_setscheduler ().
116 The scheduler makes it decisions based on knowledge of the scheduling
117 policy and static priority of all processes on the system.
118
119 For processes scheduled under one of the normal scheduling policies
120 (\fBSCHED_OTHER\fP, \fBSCHED_IDLE\fP, \fBSCHED_BATCH\fP),
121 \fIsched_priority\fP is not used in scheduling
122 decisions (it must be specified as 0).
123
124 Processes scheduled under one of the real-time policies
125 (\fBSCHED_FIFO\fP, \fBSCHED_RR\fP) have a
126 \fIsched_priority\fP value in the range 1 (low) to 99 (high).
127 (As the numbers imply, real-time processes always have higher priority
128 than normal processes.)
129 Note well: POSIX.1-2001 only requires an implementation to support a
130 minimum 32 distinct priority levels for the real-time policies,
131 and some systems supply just this minimum.
132 Portable programs should use
133 .BR sched_get_priority_min (2)
134 and
135 .BR sched_get_priority_max (2)
136 to find the range of priorities supported for a particular policy.
137
138 Conceptually, the scheduler maintains a list of runnable
139 processes for each possible \fIsched_priority\fP value.
140 In order to determine which process runs next, the scheduler looks for
141 the nonempty list with the highest static priority and selects the
142 process at the head of this list.
143
144 A process's scheduling policy determines
145 where it will be inserted into the list of processes
146 with equal static priority and how it will move inside this list.
147
148 All scheduling is preemptive: if a process with a higher static
149 priority becomes ready to run, the currently running process
150 will be preempted and
151 returned to the wait list for its static priority level.
152 The scheduling policy only determines the
153 ordering within the list of runnable processes with equal static
154 priority.
155 .SS SCHED_FIFO: First In-First Out scheduling
156 \fBSCHED_FIFO\fP can only be used with static priorities higher than
157 0, which means that when a \fBSCHED_FIFO\fP processes becomes runnable,
158 it will always immediately preempt any currently running
159 \fBSCHED_OTHER\fP, \fBSCHED_BATCH\fP, or \fBSCHED_IDLE\fP process.
160 \fBSCHED_FIFO\fP is a simple scheduling
161 algorithm without time slicing.
162 For processes scheduled under the
163 \fBSCHED_FIFO\fP policy, the following rules apply:
164 .IP * 3
165 A \fBSCHED_FIFO\fP process that has been preempted by another process of
166 higher priority will stay at the head of the list for its priority and
167 will resume execution as soon as all processes of higher priority are
168 blocked again.
169 .IP *
170 When a \fBSCHED_FIFO\fP process becomes runnable, it
171 will be inserted at the end of the list for its priority.
172 .IP *
173 A call to
174 .BR sched_setscheduler ()
175 or
176 .BR sched_setparam (2)
177 will put the
178 \fBSCHED_FIFO\fP (or \fBSCHED_RR\fP) process identified by
179 \fIpid\fP at the start of the list if it was runnable.
180 As a consequence, it may preempt the currently running process if
181 it has the same priority.
182 (POSIX.1-2001 specifies that the process should go to the end
183 of the list.)
184 .\" In 2.2.x and 2.4.x, the process is placed at the front of the queue
185 .\" In 2.0.x, the Right Thing happened: the process went to the back -- MTK
186 .IP *
187 A process calling
188 .BR sched_yield (2)
189 will be put at the end of the list.
190 .PP
191 No other events will move a process
192 scheduled under the \fBSCHED_FIFO\fP policy in the wait list of
193 runnable processes with equal static priority.
194
195 A \fBSCHED_FIFO\fP
196 process runs until either it is blocked by an I/O request, it is
197 preempted by a higher priority process, or it calls
198 .BR sched_yield (2).
199 .SS SCHED_RR: Round Robin scheduling
200 \fBSCHED_RR\fP is a simple enhancement of \fBSCHED_FIFO\fP.
201 Everything
202 described above for \fBSCHED_FIFO\fP also applies to \fBSCHED_RR\fP,
203 except that each process is only allowed to run for a maximum time
204 quantum.
205 If a \fBSCHED_RR\fP process has been running for a time
206 period equal to or longer than the time quantum, it will be put at the
207 end of the list for its priority.
208 A \fBSCHED_RR\fP process that has
209 been preempted by a higher priority process and subsequently resumes
210 execution as a running process will complete the unexpired portion of
211 its round robin time quantum.
212 The length of the time quantum can be
213 retrieved using
214 .BR sched_rr_get_interval (2).
215 .\" On Linux 2.4, the length of the RR interval is influenced
216 .\" by the process nice value -- MTK
217 .\"
218 .SS SCHED_OTHER: Default Linux time-sharing scheduling
219 \fBSCHED_OTHER\fP can only be used at static priority 0.
220 \fBSCHED_OTHER\fP is the standard Linux time-sharing scheduler that is
221 intended for all processes that do not require the special
222 real-time mechanisms.
223 The process to run is chosen from the static
224 priority 0 list based on a \fIdynamic\fP priority that is determined only
225 inside this list.
226 The dynamic priority is based on the nice value (set by
227 .BR nice (2)
228 or
229 .BR setpriority (2))
230 and increased for each time quantum the process is ready to run,
231 but denied to run by the scheduler.
232 This ensures fair progress among all \fBSCHED_OTHER\fP processes.
233 .\"
234 .SS SCHED_BATCH: Scheduling batch processes
235 (Since Linux 2.6.16.)
236 \fBSCHED_BATCH\fP can only be used at static priority 0.
237 This policy is similar to \fBSCHED_OTHER\fP in that it schedules
238 the process according to its dynamic priority
239 (based on the nice value).
240 The difference is that this policy
241 will cause the scheduler to always assume
242 that the process is CPU-intensive.
243 Consequently, the scheduler will apply a small scheduling
244 penalty with respect to wakeup behaviour,
245 so that this process is mildly disfavored in scheduling decisions.
246
247 .\" The following paragraph is drawn largely from the text that
248 .\" accompanied Ingo Molnar's patch for the implementation of
249 .\" SCHED_BATCH.
250 This policy is useful for workloads that are noninteractive,
251 but do not want to lower their nice value,
252 and for workloads that want a deterministic scheduling policy without
253 interactivity causing extra preemptions (between the workload's tasks).
254 .\"
255 .SS SCHED_IDLE: Scheduling very low priority jobs
256 (Since Linux 2.6.23.)
257 \fBSCHED_IDLE\fP can only be used at static priority 0;
258 the process nice value has no influence for this policy.
259
260 This policy is intended for running jobs at extremely low
261 priority (lower even than a +19 nice value with the
262 .B SCHED_OTHER
263 or
264 .B SCHED_BATCH
265 policies).
266 .\"
267 .SS Resetting scheduling policy for child processes
268 Since Linux 2.6.32, the
269 .B SCHED_RESET_ON_FORK
270 flag can be ORed in
271 .I policy
272 when calling
273 .BR sched_setscheduler ().
274 As a result of including this flag, children created by
275 .BR fork (2)
276 do not inherit privileged scheduling policies.
277 This feature is intended for media-playback applications,
278 and can be used to prevent applications evading the
279 .BR RLIMIT_RTTIME
280 resource limit (see
281 .BR getrlimit (2))
282 by creating multiple child processes.
283
284 More precisely, if the
285 .BR SCHED_RESET_ON_FORK
286 flag is specified,
287 the following rules apply for subsequently created children:
288 .IP * 3
289 If the calling process has a scheduling policy of
290 .B SCHED_FIFO
291 or
292 .BR SCHED_RR ,
293 the policy is reset to
294 .BR SCHED_OTHER
295 in child processes.
296 .IP *
297 If the calling process has a negative nice value,
298 the nice value is reset to zero in child processes.
299 .PP
300 After the
301 .BR SCHED_RESET_ON_FORK
302 flag has been enabled,
303 it can only be reset if the process has the
304 .BR CAP_SYS_NICE
305 capability.
306 This flag is disabled in child processes created by
307 .BR fork (2).
308
309 The
310 .B SCHED_RESET_ON_FORK
311 flag is visible in the policy value returned by
312 .BR sched_getscheduler ()
313 .\"
314 .SS Privileges and resource limits
315 In Linux kernels before 2.6.12, only privileged
316 .RB ( CAP_SYS_NICE )
317 processes can set a nonzero static priority (i.e., set a real-time
318 scheduling policy).
319 The only change that an unprivileged process can make is to set the
320 .B SCHED_OTHER
321 policy, and this can only be done if the effective user ID of the caller of
322 .BR sched_setscheduler ()
323 matches the real or effective user ID of the target process
324 (i.e., the process specified by
325 .IR pid )
326 whose policy is being changed.
327
328 Since Linux 2.6.12, the
329 .B RLIMIT_RTPRIO
330 resource limit defines a ceiling on an unprivileged process's
331 static priority for the
332 .B SCHED_RR
333 and
334 .B SCHED_FIFO
335 policies.
336 The rules for changing scheduling policy and priority are as follows:
337 .IP * 3
338 If an unprivileged process has a nonzero
339 .B RLIMIT_RTPRIO
340 soft limit, then it can change its scheduling policy and priority,
341 subject to the restriction that the priority cannot be set to a
342 value higher than the maximum of its current priority and its
343 .B RLIMIT_RTPRIO
344 soft limit.
345 .IP *
346 If the
347 .B RLIMIT_RTPRIO
348 soft limit is 0, then the only permitted changes are to lower the priority,
349 or to switch to a non-real-time policy.
350 .IP *
351 Subject to the same rules,
352 another unprivileged process can also make these changes,
353 as long as the effective user ID of the process making the change
354 matches the real or effective user ID of the target process.
355 .IP *
356 Special rules apply for the
357 .BR SCHED_IDLE :
358 an unprivileged process operating under this policy cannot
359 change its policy, regardless of the value of its
360 .BR RLIMIT_RTPRIO
361 resource limit.
362 .PP
363 Privileged
364 .RB ( CAP_SYS_NICE )
365 processes ignore the
366 .B RLIMIT_RTPRIO
367 limit; as with older kernels,
368 they can make arbitrary changes to scheduling policy and priority.
369 See
370 .BR getrlimit (2)
371 for further information on
372 .BR RLIMIT_RTPRIO .
373 .SS Response time
374 A blocked high priority process waiting for the I/O has a certain
375 response time before it is scheduled again.
376 The device driver writer
377 can greatly reduce this response time by using a "slow interrupt"
378 interrupt handler.
379 .\" as described in
380 .\" .BR request_irq (9).
381 .SS Miscellaneous
382 Child processes inherit the scheduling policy and parameters across a
383 .BR fork (2).
384 The scheduling policy and parameters are preserved across
385 .BR execve (2).
386
387 Memory locking is usually needed for real-time processes to avoid
388 paging delays; this can be done with
389 .BR mlock (2)
390 or
391 .BR mlockall (2).
392
393 Since a nonblocking infinite loop in a process scheduled under
394 \fBSCHED_FIFO\fP or \fBSCHED_RR\fP will block all processes with lower
395 priority forever, a software developer should always keep available on
396 the console a shell scheduled under a higher static priority than the
397 tested application.
398 This will allow an emergency kill of tested
399 real-time applications that do not block or terminate as expected.
400 See also the description of the
401 .BR RLIMIT_RTTIME
402 resource limit in
403 .BR getrlimit (2).
404
405 POSIX systems on which
406 .BR sched_setscheduler ()
407 and
408 .BR sched_getscheduler ()
409 are available define
410 .B _POSIX_PRIORITY_SCHEDULING
411 in \fI<unistd.h>\fP.
412 .SH "RETURN VALUE"
413 On success,
414 .BR sched_setscheduler ()
415 returns zero.
416 On success,
417 .BR sched_getscheduler ()
418 returns the policy for the process (a nonnegative integer).
419 On error, \-1 is returned, and
420 .I errno
421 is set appropriately.
422 .SH ERRORS
423 .TP
424 .B EINVAL
425 The scheduling \fIpolicy\fP is not one of the recognized policies,
426 \fIparam\fP is NULL,
427 or \fIparam\fP does not make sense for the \fIpolicy\fP.
428 .TP
429 .B EPERM
430 The calling process does not have appropriate privileges.
431 .TP
432 .B ESRCH
433 The process whose ID is \fIpid\fP could not be found.
434 .SH "CONFORMING TO"
435 POSIX.1-2001 (but see BUGS below).
436 The \fBSCHED_BATCH\fP and \fBSCHED_IDLE\fP policies are Linux-specific.
437 .SH NOTES
438 POSIX.1 does not detail the permissions that an unprivileged
439 process requires in order to call
440 .BR sched_setscheduler (),
441 and details vary across systems.
442 For example, the Solaris 7 manual page says that
443 the real or effective user ID of the calling process must
444 match the real user ID or the save set-user-ID of the target process.
445 .PP
446 Originally, Standard Linux was intended as a general-purpose operating
447 system being able to handle background processes, interactive
448 applications, and less demanding real-time applications (applications that
449 need to usually meet timing deadlines).
450 Although the Linux kernel 2.6
451 allowed for kernel preemption and the newly introduced O(1) scheduler
452 ensures that the time needed to schedule is fixed and deterministic
453 irrespective of the number of active tasks, true real-time computing
454 was not possible up to kernel version 2.6.17.
455 .SS Real-time features in the mainline Linux kernel
456 .\" FIXME . Probably this text will need some minor tweaking
457 .\" by about the time of 2.6.30; ask Carsten Emde about this then.
458 From kernel version 2.6.18 onward, however, Linux is gradually
459 becoming equipped with real-time capabilities,
460 most of which are derived from the former
461 .I realtime-preempt
462 patches developed by Ingo Molnar, Thomas Gleixner,
463 Steven Rostedt, and others.
464 Until the patches have been completely merged into the
465 mainline kernel
466 (this is expected to be around kernel version 2.6.30),
467 they must be installed to achieve the best real-time performance.
468 These patches are named:
469 .in +4n
470 .nf
471
472 patch-\fIkernelversion\fP-rt\fIpatchversion\fP
473 .fi
474 .in
475 .PP
476 and can be downloaded from
477 .IR http://www.kernel.org/pub/linux/kernel/projects/rt/ .
478
479 Without the patches and prior to their full inclusion into the mainline
480 kernel, the kernel configuration offers only the three preemption classes
481 .BR CONFIG_PREEMPT_NONE ,
482 .BR CONFIG_PREEMPT_VOLUNTARY ,
483 and
484 .B CONFIG_PREEMPT_DESKTOP
485 which respectively provide no, some, and considerable
486 reduction of the worst-case scheduling latency.
487
488 With the patches applied or after their full inclusion into the mainline
489 kernel, the additional configuration item
490 .B CONFIG_PREEMPT_RT
491 becomes available.
492 If this is selected, Linux is transformed into a regular
493 real-time operating system.
494 The FIFO and RR scheduling policies that can be selected using
495 .BR sched_setscheduler ()
496 are then used to run a process
497 with true real-time priority and a minimum worst-case scheduling latency.
498 .SH BUGS
499 POSIX says that on success,
500 .BR sched_setscheduler ()
501 should return the previous scheduling policy.
502 Linux
503 .BR sched_setscheduler ()
504 does not conform to this requirement,
505 since it always returns 0 on success.
506 .SH "SEE ALSO"
507 .BR getpriority (2),
508 .BR mlock (2),
509 .BR mlockall (2),
510 .BR munlock (2),
511 .BR munlockall (2),
512 .BR nice (2),
513 .BR sched_get_priority_max (2),
514 .BR sched_get_priority_min (2),
515 .BR sched_getaffinity (2),
516 .BR sched_getparam (2),
517 .BR sched_rr_get_interval (2),
518 .BR sched_setaffinity (2),
519 .BR sched_setparam (2),
520 .BR sched_yield (2),
521 .BR setpriority (2),
522 .BR capabilities (7),
523 .BR cpuset (7)
524 .PP
525 .I Programming for the real world \- POSIX.4
526 by Bill O. Gallmeister, O'Reilly & Associates, Inc., ISBN 1-56592-074-0
527 .PP
528 The kernel source file
529 .I Documentation/scheduler/sched-rt-group.txt
530 (since kernel 2.6.25).