sched_setattr(2)
sched_setattr(2) โ System Calls Manual, but make it cute ๐
okay so bestie, we are deep in the Linux System Calls Manual and this page is sched_setattr(2). Itโs giving low-level scheduler energy, and yes itโs long, but weโre gonna get through it together. The two syscalls here are sched_setattr and sched_getattr โ set and get scheduling policy and attributes.
โจ NAME โจ
sched_setattr, sched_getattr โ set and get scheduling policy and attributes
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ช SYNOPSIS ๐ช
#include <sched.h> /* Definition of SCHED_* constants */
#include <sys/syscall.h> /* Definition of SYS_* constants */
#include <unistd.h>
int syscall(SYS_sched_setattr, pid_t pid, struct sched_attr *attr,
unsigned int flags);
int syscall(SYS_sched_getattr, pid_t pid, struct sched_attr *attr,
unsigned int size, unsigned int flags);
๐จ Note bestie: glibc provides no wrappers for these system calls, necessitating the use of syscall(2).
๐ธ DESCRIPTION ๐ธ
๐ sched_setattr() ๐
The sched_setattr() system call sets the scheduling policy and associated attributes for the thread whose ID is specified in pid. If pid equals zero, the scheduling policy and attributes of the calling thread will be set.
Currently, Linux supports the following "normal" (i.e., non-real-time) scheduling policies as values that may be specified in policy:
SCHED_OTHERโ the standard round-robin time-sharing policy;SCHED_BATCHโ for"batch"style execution of processes; andSCHED_IDLEโ for running very low priority background jobs.
Various "real-time" policies are also supported, for special time-critical applications that need precise control over the way in which runnable threads are selected for execution. For the rules governing when a process may use these policies, see sched(7). The real-time policies that may be specified in policy are:
SCHED_FIFOโ a first-in, first-out policy; andSCHED_RRโ a round-robin policy.
Linux also provides the following policy:
SCHED_DEADLINEโ a deadline scheduling policy; seesched(7)for details.
The attr argument is a pointer to a structure that defines the new scheduling policy and attributes for the specified thread. This structure has the following form:
struct sched_attr {
u32 size; /* Size of this structure */
u32 sched_policy; /* Policy (SCHED_*) */
u64 sched_flags; /* Flags */
s32 sched_nice; /* Nice value (SCHED_OTHER,
SCHED_BATCH) */
u32 sched_priority; /* Static priority (SCHED_FIFO,
SCHED_RR) */
/* For SCHED_DEADLINE */
u64 sched_runtime;
u64 sched_deadline;
u64 sched_period;
/* Utilization hints */
u32 sched_util_min;
u32 sched_util_max;
};
The fields of the sched_attr structure are as follows:
sizeโ This field should be set to the size of the structure in bytes, as insizeof(struct sched_attr). If the provided structure is smaller than the kernel structure, any additional fields are assumed to be'0'. If the provided structure is larger than the kernel structure, the kernel verifies that all additional fields are 0; if they are not,sched_setattr()fails with the errorE2BIGand updatessizeto contain the size of the kernel structure.
The above behavior when the size of the user-space sched_attr structure does not match the size of the kernel structure allows for future extensibility of the interface. Malformed applications that pass oversize structures won't break in the future if the size of the kernel sched_attr structure is increased. In the future, it could also allow applications that know about a larger user-space sched_attr structure to determine whether they are running on an older kernel that does not support the larger structure.
sched_policyโ This field specifies the scheduling policy, as one of theSCHED_*values listed above.sched_flagsโ This field contains zero or more of the following flags that are ORed together to control scheduling behavior:SCHED_FLAG_RESET_ON_FORKโ Children created byfork(2)do not inherit privileged scheduling policies. Seesched(7)for details.SCHED_FLAG_RECLAIM(since Linux 4.13) โ This flag allows aSCHED_DEADLINEthread to reclaim bandwidth unused by other real-time threads.SCHED_FLAG_DL_OVERRUN(since Linux 4.16) โ This flag allows an application to get informed about run-time overruns inSCHED_DEADLINEthreads. Such overruns may be caused by (for example) coarse execution time accounting or incorrect parameter assignment. Notification takes the form of aSIGXCPUsignal which is generated on each overrun.
This SIGXCPU signal is process-directed (see signal(7)) rather than thread-directed. This is probably a bug. On the one hand, sched_setattr() is being used to set a per-thread attribute. On the other hand, if the process-directed signal is delivered to a thread inside the process other than the one that had a run-time overrun, the application has no way of knowing which thread overran.
SCHED_FLAG_UTIL_CLAMP_MIN,SCHED_FLAG_UTIL_CLAMP_MAX(both since Linux 5.3) โ These flags indicate that thesched_util_minorsched_util_maxfields, respectively, are present, representing the expected minimum and maximum utilization of the thread.
The utilization attributes provide the scheduler with boundaries within which it should schedule the thread, potentially informing its decisions regarding task placement and frequency selection.
sched_niceโ This field specifies the nice value to be set when specifyingsched_policyasSCHED_OTHERorSCHED_BATCH. The nice value is a number in the range-20(high priority) to+19(low priority); seesched(7).sched_priorityโ This field specifies the static priority to be set when specifyingsched_policyasSCHED_FIFOorSCHED_RR. The allowed range of priorities for these policies can be determined usingsched_get_priority_min(2)andsched_get_priority_max(2). For other policies, this field must be specified as0.sched_runtimeโ This field specifies the"Runtime"parameter for deadline scheduling. The value is expressed in nanoseconds. This field, and the next two fields, are used only forSCHED_DEADLINEscheduling; for further details, seesched(7).sched_deadlineโ This field specifies the"Deadline"parameter for deadline scheduling. The value is expressed in nanoseconds.sched_periodโ This field specifies the"Period"parameter for deadline scheduling. The value is expressed in nanoseconds.sched_util_min,sched_util_max(both since Linux 5.3) โ These fields specify the expected minimum and maximum utilization, respectively. They are ignored unless their correspondingSCHED_FLAG_UTIL_CLAMP_MINorSCHED_FLAG_UTIL_CLAMP_MAXis set insched_flags.
Utilization is a value in the range [0, 1024], representing the percentage of CPU time used by a task when running at the maximum frequency on the highest capacity CPU of the system. This is a fixed point representation, where 1024 corresponds to 100%, and 0 corresponds to 0%. For example, a 20% utilization task is a task running for 2ms every 10ms at maximum frequency and is represented by a utilization value of 0.2 * 1024 = 205.
A task with a minimum utilization value larger than 0 is more likely scheduled on a CPU with a capacity big enough to fit the specified value. A task with a maximum utilization value smaller than 1024 is more likely scheduled on a CPU with no more capacity than the specified value.
A task utilization boundary can be reset by setting its field to UINT32_MAX (since Linux 5.11).
The flags argument is provided to allow for future extensions to the interface; in the current implementation it must be specified as 0.
๐ sched_getattr() ๐
The sched_getattr() system call fetches the scheduling policy and the associated attributes for the thread whose ID is specified in pid. If pid equals zero, the scheduling policy and attributes of the calling thread will be retrieved.
The size argument should be set to the size of the sched_attr structure as known to user space. The value must be at least as large as the size of the initially published sched_attr structure, or the call fails with the error EINVAL.
The retrieved scheduling attributes are placed in the fields of the sched_attr structure pointed to by attr. The kernel sets attr.size to the size of its sched_attr structure.
If the caller-provided attr buffer is larger than the kernel's sched_attr structure, the additional bytes in the user-space structure are not touched. If the caller-provided structure is smaller than the kernel sched_attr structure, the kernel will silently not return any values which would be stored outside the provided space. As with sched_setattr(), these semantics allow for future extensibility of the interface.
The flags argument is provided to allow for future extensions to the interface; in the current implementation it must be specified as 0.
๐ RETURN VALUE ๐
On success, sched_setattr() and sched_getattr() return 0. On error, -1 is returned, and errno is set to indicate the error.
๐ ERRORS ๐
okay bestie, so both sched_getattr() and sched_setattr() can flop for these reasons ๐
EINVALโattrisNULL; orpidis negative; orflagsis not zero. Basically you handed it something cursed.ESRCHโ the thread whose ID ispidcould not be found. She's not here, bestie. ๐
๐ธ and also, sched_getattr() specifically can fail like this:
E2BIGโ the buffer you specified withsizeandattris too small. Not enough room, no cap. ๐EINVALโsizeis invalid; that is, it is smaller than the initial version of thesched_attrstructure (48bytes) or larger than the system page size.
๐ฅ and sched_setattr() specifically can fail like this:
E2BIGโ the buffer specified bysizeandattris larger than the kernel structure, and one or more of the excess bytes is nonzero.EBUSYโSCHED_DEADLINEadmission control failure, seesched(7).EINVALโattr.sched_policyis not one of the recognized policies.EINVALโattr.sched_flagscontains a flag other thanSCHED_FLAG_RESET_ON_FORK.EINVALโattr.sched_priorityis invalid.EINVALโattr.sched_policyisSCHED_DEADLINE, and the deadline scheduling parameters inattrare invalid.EINVALโattr.sched_flagscontainsSCHED_FLAG_UTIL_CLAMP_MINorSCHED_FLAG_UTIL_CLAMP_MAX, andattr.sched_util_minorattr.sched_util_maxare out of bounds.EOPNOTSUPPโSCHED_FLAG_UTIL_CLAMPwas provided, but the kernel was not built withCONFIG_UCLAMP_TASKsupport. she literally cannot, bestieEPERMโ the caller does not have appropriate privileges.EPERMโ the CPU affinity mask of the thread specified bypiddoes not include all CPUs in the system (seesched_setaffinity(2)).
โจ STANDARDS โจ
Linux. that's it, that's the whole section ๐
๐ฆ HISTORY ๐ฆ
Linux 3.14.
๐ NOTES ๐
glibc does not provide wrappers for these system calls โ so you gotta call them using syscall(2). iykyk ๐ญ
sched_setattr() provides a superset of the functionality of sched_setscheduler(2), sched_setparam(2), nice(2), and (other than the ability to set the priority of all processes belonging to a specified user or all processes in a specified group) setpriority(2).
Analogously, sched_getattr() provides a superset of the functionality of sched_getscheduler(2), sched_getparam(2), and (partially) getpriority(2). she does it all, periodt ๐
๐ BUGS ๐
- In Linux versions up to
3.15,sched_setattr()failed with the errorEFAULTinstead ofE2BIGfor the case described in ERRORS. yeah, old kernels were messy fr. - Up to Linux
5.3,sched_getattr()failed with the errorEFBIGif the in-kernelsched_attrstructure was larger than the size passed by user space.
๐ SEE ALSO ๐
chrt(1), nice(2), sched_get_priority_max(2), sched_get_priority_min(2), sched_getaffinity(2), sched_getparam(2), sched_getscheduler(2), sched_rr_get_interval(2), sched_setaffinity(2), sched_setparam(2), sched_setscheduler(2), sched_yield(2), setpriority(2), pthread_getschedparam(3), pthread_setschedparam(3), pthread_setschedprio(3), capabilities(7), cpuset(7), sched(7)
and that's the whole page, bestie ๐ซถ this lil masterpiece lives in Linux man-pages 6.20, dated 2026-02-08. go schedule something iconic ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2014, Michael Kerrisk <mtk.manpages@gmail.com>
- Copyright 2014, Peter Zijlstra <peterz@infradead.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms