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