1 .\" Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
2 .\" and Copyright (C) 2014 Peter Zijlstra <peterz@infradead.org>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH SCHED_SETATTR 2 2022-09-09 "Linux man-pages (unreleased)"
8 sched_setattr, sched_getattr \-
9 set and get scheduling policy and attributes
12 .RI ( libc ", " \-lc )
15 .BR "#include <sched.h>" " /* Definition of " SCHED_* " constants */"
16 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
17 .B #include <unistd.h>
19 .BI "int syscall(SYS_sched_setattr, pid_t " pid ", struct sched_attr *" attr ,
20 .BI " unsigned int " flags );
21 .BI "int syscall(SYS_sched_getattr, pid_t " pid ", struct sched_attr *" attr ,
22 .BI " unsigned int " size ", unsigned int " flags );
24 .\" FIXME . Add feature test macro requirements
27 glibc provides no wrappers for these system calls,
28 necessitating the use of
34 system call sets the scheduling policy and
35 associated attributes for the thread whose ID is specified in
40 the scheduling policy and attributes of the calling thread will be set.
42 Currently, Linux supports the following "normal"
43 (i.e., non-real-time) scheduling policies as values that may be specified in
47 the standard round-robin time-sharing policy;
48 .\" In the 2.6 kernel sources, SCHED_OTHER is actually called
52 for "batch" style execution of processes; and
57 low priority background jobs.
59 Various "real-time" policies are also supported,
60 for special time-critical applications that need precise control over
61 the way in which runnable threads are selected for execution.
62 For the rules governing when a process may use these policies, see
64 The real-time policies that may be specified in
69 a first-in, first-out policy; and
74 Linux also provides the following policy:
77 a deadline scheduling policy; see
83 argument is a pointer to a structure that defines
84 the new scheduling policy and attributes for the specified thread.
85 This structure has the following form:
90 u32 size; /* Size of this structure */
91 u32 sched_policy; /* Policy (SCHED_*) */
92 u64 sched_flags; /* Flags */
93 s32 sched_nice; /* Nice value (SCHED_OTHER,
95 u32 sched_priority; /* Static priority (SCHED_FIFO,
97 /* Remaining fields are for SCHED_DEADLINE */
107 structure are as follows:
110 This field should be set to the size of the structure in bytes, as in
111 .IR "sizeof(struct sched_attr)" .
112 If the provided structure is smaller than the kernel structure,
113 any additional fields are assumed to be '0'.
114 If the provided structure is larger than the kernel structure,
115 the kernel verifies that all additional fields are 0;
122 to contain the size of the kernel structure.
124 The above behavior when the size of the user-space
126 structure does not match the size of the kernel structure
127 allows for future extensibility of the interface.
128 Malformed applications that pass oversize structures
129 won't break in the future if the size of the kernel
131 structure is increased.
133 it could also allow applications that know about a larger user-space
135 structure to determine whether they are running on an older kernel
136 that does not support the larger structure.
139 This field specifies the scheduling policy, as one of the
144 This field contains zero or more of the following flags
145 that are ORed together to control scheduling behavior:
148 .B SCHED_FLAG_RESET_ON_FORK
151 do not inherit privileged scheduling policies.
156 .BR SCHED_FLAG_RECLAIM " (since Linux 4.13)"
157 .\" 2d4283e9d583a3ee8cfb1cbb9c1270614df4c29d
160 thread to reclaim bandwidth unused by other real-time threads.
161 .\" Bandwidth reclaim is done via the GRUB algorithm; see
162 .\" Documentation/scheduler/sched-deadline.txt
164 .BR SCHED_FLAG_DL_OVERRUN " (since Linux 4.16)"
165 .\" commit 34be39305a77b8b1ec9f279163c7cdb6cc719b91
166 This flag allows an application to get informed about run-time overruns in
169 Such overruns may be caused by (for example) coarse execution time accounting
170 or incorrect parameter assignment.
171 Notification takes the form of a
173 signal which is generated on each overrun.
181 rather than thread-directed.
182 This is probably a bug.
185 is being used to set a per-thread attribute.
186 On the other hand, if the process-directed signal is delivered to
187 a thread inside the process other than the one that had a run-time overrun,
188 the application has no way of knowing which thread overran.
192 This field specifies the nice value to be set when specifying
198 The nice value is a number in the range \-20 (high priority)
199 to +19 (low priority); see
203 This field specifies the static priority to be set when specifying
209 The allowed range of priorities for these policies can be determined using
210 .BR sched_get_priority_min (2)
212 .BR sched_get_priority_max (2).
213 For other policies, this field must be specified as 0.
216 This field specifies the "Runtime" parameter for deadline scheduling.
217 The value is expressed in nanoseconds.
218 This field, and the next two fields,
221 scheduling; for further details, see
225 This field specifies the "Deadline" parameter for deadline scheduling.
226 The value is expressed in nanoseconds.
229 This field specifies the "Period" parameter for deadline scheduling.
230 The value is expressed in nanoseconds.
234 argument is provided to allow for future extensions to the interface;
235 in the current implementation it must be specified as 0.
241 system call fetches the scheduling policy and the
242 associated attributes for the thread whose ID is specified in
247 the scheduling policy and attributes of the calling thread
252 argument should be set to the size of the
254 structure as known to user space.
255 The value must be at least as large as the size of the initially published
257 structure, or the call fails with the error
260 The retrieved scheduling attributes are placed in the fields of the
262 structure pointed to by
270 If the caller-provided
272 buffer is larger than the kernel's
275 the additional bytes in the user-space structure are not touched.
276 If the caller-provided structure is smaller than the kernel
278 structure, the kernel will silently not return any values which would be stored
279 outside the provided space.
281 .BR sched_setattr (),
282 these semantics allow for future extensibility of the interface.
286 argument is provided to allow for future extensions to the interface;
287 in the current implementation it must be specified as 0.
294 On error, \-1 is returned, and
296 is set to indicate the error.
301 can both fail for the following reasons:
312 The thread whose ID is
318 can fail for the following reasons:
321 The buffer specified by
329 is invalid; that is, it is smaller than the initial version of the
331 structure (48 bytes) or larger than the system page size.
335 can fail for the following reasons:
338 The buffer specified by
342 is larger than the kernel structure,
343 and one or more of the excess bytes is nonzero.
347 admission control failure, see
352 is not one of the recognized policies;
354 contains a flag other than
355 .BR SCHED_FLAG_RESET_ON_FORK ;
357 .I attr.sched_priority
362 and the deadline scheduling parameters in
367 The caller does not have appropriate privileges.
370 The CPU affinity mask of the thread specified by
372 does not include all CPUs in the system
374 .BR sched_setaffinity (2)).
376 These system calls first appeared in Linux 3.14.
377 .\" FIXME . Add glibc version
379 These system calls are nonstandard Linux extensions.
381 Glibc does not provide wrappers for these system calls; call them using
385 provides a superset of the functionality of
386 .BR sched_setscheduler (2),
387 .BR sched_setparam (2),
389 and (other than the ability to set the priority of all processes
390 belonging to a specified user or all processes in a specified group)
394 provides a superset of the functionality of
395 .BR sched_getscheduler (2),
396 .BR sched_getparam (2),
400 In Linux versions up to
401 .\" FIXME . patch sent to Peter Zijlstra
404 failed with the error
408 for the case described in ERRORS.
410 In Linux versions up to 5.3,
412 failed with the error
416 structure was larger than the
418 passed by user space.
419 .\" In Linux versions up to up 3.15,
420 .\" FIXME . patch from Peter Zijlstra pending
421 .\" .BR sched_setattr ()
422 .\" allowed a negative
423 .\" .I attr.sched_policy
430 .BR sched_get_priority_max (2),
431 .BR sched_get_priority_min (2),
432 .BR sched_getaffinity (2),
433 .BR sched_getparam (2),
434 .BR sched_getscheduler (2),
435 .BR sched_rr_get_interval (2),
436 .BR sched_setaffinity (2),
437 .BR sched_setparam (2),
438 .BR sched_setscheduler (2),
441 .BR pthread_getschedparam (3),
442 .BR pthread_setschedparam (3),
443 .BR pthread_setschedprio (3),
444 .BR capabilities (7),