1SCHED_SETATTR(2) Linux Programmer's Manual SCHED_SETATTR(2)
2
3
4
6 sched_setattr, sched_getattr - set and get scheduling policy and at‐
7 tributes
8
10 #include <sched.h> /* Definition of SCHED_* constants */
11 #include <sys/syscall.h> /* Definition of SYS_* constants */
12 #include <unistd.h>
13
14 int syscall(SYS_sched_setattr, pid_t pid, struct sched_attr *attr,
15 unsigned int flags);
16 int syscall(SYS_sched_getattr, pid_t pid, struct sched_attr *attr,
17 unsigned int size, unsigned int flags);
18
19 Note: glibc provides no wrappers for these system calls, necessitating
20 the use of syscall(2).
21
23 sched_setattr()
24 The sched_setattr() system call sets the scheduling policy and associ‐
25 ated attributes for the thread whose ID is specified in pid. If pid
26 equals zero, the scheduling policy and attributes of the calling thread
27 will be set.
28
29 Currently, Linux supports the following "normal" (i.e., non-real-time)
30 scheduling policies as values that may be specified in policy:
31
32 SCHED_OTHER the standard round-robin time-sharing policy;
33
34 SCHED_BATCH for "batch" style execution of processes; and
35
36 SCHED_IDLE for running very low priority background jobs.
37
38 Various "real-time" policies are also supported, for special time-crit‐
39 ical applications that need precise control over the way in which
40 runnable threads are selected for execution. For the rules governing
41 when a process may use these policies, see sched(7). The real-time
42 policies that may be specified in policy are:
43
44 SCHED_FIFO a first-in, first-out policy; and
45
46 SCHED_RR a round-robin policy.
47
48 Linux also provides the following policy:
49
50 SCHED_DEADLINE
51 a deadline scheduling policy; see sched(7) for details.
52
53 The attr argument is a pointer to a structure that defines the new
54 scheduling policy and attributes for the specified thread. This struc‐
55 ture has the following form:
56
57 struct sched_attr {
58 u32 size; /* Size of this structure */
59 u32 sched_policy; /* Policy (SCHED_*) */
60 u64 sched_flags; /* Flags */
61 s32 sched_nice; /* Nice value (SCHED_OTHER,
62 SCHED_BATCH) */
63 u32 sched_priority; /* Static priority (SCHED_FIFO,
64 SCHED_RR) */
65 /* Remaining fields are for SCHED_DEADLINE */
66 u64 sched_runtime;
67 u64 sched_deadline;
68 u64 sched_period;
69 };
70
71 The fields of the sched_attr structure are as follows:
72
73 size This field should be set to the size of the structure in bytes,
74 as in sizeof(struct sched_attr). If the provided structure is
75 smaller than the kernel structure, any additional fields are as‐
76 sumed to be '0'. If the provided structure is larger than the
77 kernel structure, the kernel verifies that all additional fields
78 are 0; if they are not, sched_setattr() fails with the error
79 E2BIG and updates size to contain the size of the kernel struc‐
80 ture.
81
82 The above behavior when the size of the user-space sched_attr
83 structure does not match the size of the kernel structure allows
84 for future extensibility of the interface. Malformed applica‐
85 tions that pass oversize structures won't break in the future if
86 the size of the kernel sched_attr structure is increased. In
87 the future, it could also allow applications that know about a
88 larger user-space sched_attr structure to determine whether they
89 are running on an older kernel that does not support the larger
90 structure.
91
92 sched_policy
93 This field specifies the scheduling policy, as one of the
94 SCHED_* values listed above.
95
96 sched_flags
97 This field contains zero or more of the following flags that are
98 ORed together to control scheduling behavior:
99
100 SCHED_FLAG_RESET_ON_FORK
101 Children created by fork(2) do not inherit privileged
102 scheduling policies. See sched(7) for details.
103
104 SCHED_FLAG_RECLAIM (since Linux 4.13)
105 This flag allows a SCHED_DEADLINE thread to reclaim band‐
106 width unused by other real-time threads.
107
108 SCHED_FLAG_DL_OVERRUN (since Linux 4.16)
109 This flag allows an application to get informed about
110 run-time overruns in SCHED_DEADLINE threads. Such over‐
111 runs may be caused by (for example) coarse execution time
112 accounting or incorrect parameter assignment. Notifica‐
113 tion takes the form of a SIGXCPU signal which is gener‐
114 ated on each overrun.
115
116 This SIGXCPU signal is process-directed (see signal(7))
117 rather than thread-directed. This is probably a bug. On
118 the one hand, sched_setattr() is being used to set a per-
119 thread attribute. On the other hand, if the process-di‐
120 rected signal is delivered to a thread inside the process
121 other than the one that had a run-time overrun, the ap‐
122 plication has no way of knowing which thread overran.
123
124 sched_nice
125 This field specifies the nice value to be set when specifying
126 sched_policy as SCHED_OTHER or SCHED_BATCH. The nice value is a
127 number in the range -20 (high priority) to +19 (low priority);
128 see sched(7).
129
130 sched_priority
131 This field specifies the static priority to be set when specify‐
132 ing sched_policy as SCHED_FIFO or SCHED_RR. The allowed range
133 of priorities for these policies can be determined using
134 sched_get_priority_min(2) and sched_get_priority_max(2). For
135 other policies, this field must be specified as 0.
136
137 sched_runtime
138 This field specifies the "Runtime" parameter for deadline sched‐
139 uling. The value is expressed in nanoseconds. This field, and
140 the next two fields, are used only for SCHED_DEADLINE schedul‐
141 ing; for further details, see sched(7).
142
143 sched_deadline
144 This field specifies the "Deadline" parameter for deadline
145 scheduling. The value is expressed in nanoseconds.
146
147 sched_period
148 This field specifies the "Period" parameter for deadline sched‐
149 uling. The value is expressed in nanoseconds.
150
151 The flags argument is provided to allow for future extensions to the
152 interface; in the current implementation it must be specified as 0.
153
154 sched_getattr()
155 The sched_getattr() system call fetches the scheduling policy and the
156 associated attributes for the thread whose ID is specified in pid. If
157 pid equals zero, the scheduling policy and attributes of the calling
158 thread will be retrieved.
159
160 The size argument should be set to the size of the sched_attr structure
161 as known to user space. The value must be at least as large as the
162 size of the initially published sched_attr structure, or the call fails
163 with the error EINVAL.
164
165 The retrieved scheduling attributes are placed in the fields of the
166 sched_attr structure pointed to by attr. The kernel sets attr.size to
167 the size of its sched_attr structure.
168
169 If the caller-provided attr buffer is larger than the kernel's
170 sched_attr structure, the additional bytes in the user-space structure
171 are not touched. If the caller-provided structure is smaller than the
172 kernel sched_attr structure, the kernel will silently not return any
173 values which would be stored outside the provided space. As with
174 sched_setattr(), these semantics allow for future extensibility of the
175 interface.
176
177 The flags argument is provided to allow for future extensions to the
178 interface; in the current implementation it must be specified as 0.
179
181 On success, sched_setattr() and sched_getattr() return 0. On error, -1
182 is returned, and errno is set to indicate the error.
183
185 sched_getattr() and sched_setattr() can both fail for the following
186 reasons:
187
188 EINVAL attr is NULL; or pid is negative; or flags is not zero.
189
190 ESRCH The thread whose ID is pid could not be found.
191
192 In addition, sched_getattr() can fail for the following reasons:
193
194 E2BIG The buffer specified by size and attr is too small.
195
196 EINVAL size is invalid; that is, it is smaller than the initial version
197 of the sched_attr structure (48 bytes) or larger than the system
198 page size.
199
200 In addition, sched_setattr() can fail for the following reasons:
201
202 E2BIG The buffer specified by size and attr is larger than the kernel
203 structure, and one or more of the excess bytes is nonzero.
204
205 EBUSY SCHED_DEADLINE admission control failure, see sched(7).
206
207 EINVAL attr.sched_policy is not one of the recognized policies;
208 attr.sched_flags contains a flag other than SCHED_FLAG_RE‐
209 SET_ON_FORK; or attr.sched_priority is invalid; or
210 attr.sched_policy is SCHED_DEADLINE and the deadline scheduling
211 parameters in attr are invalid.
212
213 EPERM The caller does not have appropriate privileges.
214
215 EPERM The CPU affinity mask of the thread specified by pid does not
216 include all CPUs in the system (see sched_setaffinity(2)).
217
219 These system calls first appeared in Linux 3.14.
220
222 These system calls are nonstandard Linux extensions.
223
225 Glibc does not provide wrappers for these system calls; call them using
226 syscall(2).
227
228 sched_setattr() provides a superset of the functionality of
229 sched_setscheduler(2), sched_setparam(2), nice(2), and (other than the
230 ability to set the priority of all processes belonging to a specified
231 user or all processes in a specified group) setpriority(2). Analo‐
232 gously, sched_getattr() provides a superset of the functionality of
233 sched_getscheduler(2), sched_getparam(2), and (partially) getprior‐
234 ity(2).
235
237 In Linux versions up to 3.15, sched_setattr() failed with the error
238 EFAULT instead of E2BIG for the case described in ERRORS.
239
240 In Linux versions up to 5.3, sched_getattr() failed with the error EF‐
241 BIG if the in-kernel sched_attr structure was larger than the size
242 passed by user space.
243
245 chrt(1), nice(2), sched_get_priority_max(2), sched_get_priority_min(2),
246 sched_getaffinity(2), sched_getparam(2), sched_getscheduler(2),
247 sched_rr_get_interval(2), sched_setaffinity(2), sched_setparam(2),
248 sched_setscheduler(2), sched_yield(2), setpriority(2),
249 pthread_getschedparam(3), pthread_setschedparam(3),
250 pthread_setschedprio(3), capabilities(7), cpuset(7), sched(7)
251
253 This page is part of release 5.13 of the Linux man-pages project. A
254 description of the project, information about reporting bugs, and the
255 latest version of this page, can be found at
256 https://www.kernel.org/doc/man-pages/.
257
258
259
260Linux 2021-03-22 SCHED_SETATTR(2)