1sched_setscheduler(2) System Calls Manual sched_setscheduler(2)
2
3
4
6 sched_setscheduler, sched_getscheduler - set and get scheduling pol‐
7 icy/parameters
8
10 Standard C library (libc, -lc)
11
13 #include <sched.h>
14
15 int sched_setscheduler(pid_t pid, int policy,
16 const struct sched_param *param);
17 int sched_getscheduler(pid_t pid);
18
20 The sched_setscheduler() system call sets both the scheduling policy
21 and parameters for the thread whose ID is specified in pid. If pid
22 equals zero, the scheduling policy and parameters of the calling thread
23 will be set.
24
25 The scheduling parameters are specified in the param argument, which is
26 a pointer to a structure of the following form:
27
28 struct sched_param {
29 ...
30 int sched_priority;
31 ...
32 };
33
34 In the current implementation, the structure contains only one field,
35 sched_priority. The interpretation of param depends on the selected
36 policy.
37
38 Currently, Linux supports the following "normal" (i.e., non-real-time)
39 scheduling policies as values that may be specified in policy:
40
41 SCHED_OTHER the standard round-robin time-sharing policy;
42
43 SCHED_BATCH for "batch" style execution of processes; and
44
45 SCHED_IDLE for running very low priority background jobs.
46
47 For each of the above policies, param->sched_priority must be 0.
48
49 Various "real-time" policies are also supported, for special time-crit‐
50 ical applications that need precise control over the way in which
51 runnable threads are selected for execution. For the rules governing
52 when a process may use these policies, see sched(7). The real-time
53 policies that may be specified in policy are:
54
55 SCHED_FIFO a first-in, first-out policy; and
56
57 SCHED_RR a round-robin policy.
58
59 For each of the above policies, param->sched_priority specifies a
60 scheduling priority for the thread. This is a number in the range re‐
61 turned by calling sched_get_priority_min(2) and sched_get_prior‐
62 ity_max(2) with the specified policy. On Linux, these system calls re‐
63 turn, respectively, 1 and 99.
64
65 Since Linux 2.6.32, the SCHED_RESET_ON_FORK flag can be ORed in policy
66 when calling sched_setscheduler(). As a result of including this flag,
67 children created by fork(2) do not inherit privileged scheduling poli‐
68 cies. See sched(7) for details.
69
70 sched_getscheduler() returns the current scheduling policy of the
71 thread identified by pid. If pid equals zero, the policy of the call‐
72 ing thread will be retrieved.
73
75 On success, sched_setscheduler() returns zero. On success,
76 sched_getscheduler() returns the policy for the thread (a nonnegative
77 integer). On error, both calls return -1, and errno is set to indicate
78 the error.
79
81 EINVAL Invalid arguments: pid is negative or param is NULL.
82
83 EINVAL (sched_setscheduler()) policy is not one of the recognized poli‐
84 cies.
85
86 EINVAL (sched_setscheduler()) param does not make sense for the speci‐
87 fied policy.
88
89 EPERM The calling thread does not have appropriate privileges.
90
91 ESRCH The thread whose ID is pid could not be found.
92
94 POSIX.1 does not detail the permissions that an unprivileged thread re‐
95 quires in order to call sched_setscheduler(), and details vary across
96 systems. For example, the Solaris 7 manual page says that the real or
97 effective user ID of the caller must match the real user ID or the save
98 set-user-ID of the target.
99
100 The scheduling policy and parameters are in fact per-thread attributes
101 on Linux. The value returned from a call to gettid(2) can be passed in
102 the argument pid. Specifying pid as 0 will operate on the attributes
103 of the calling thread, and passing the value returned from a call to
104 getpid(2) will operate on the attributes of the main thread of the
105 thread group. (If you are using the POSIX threads API, then use
106 pthread_setschedparam(3), pthread_getschedparam(3), and
107 pthread_setschedprio(3), instead of the sched_*[22m(2) system calls.)
108
110 POSIX.1-2008 (but see BUGS below).
111
112 SCHED_BATCH and SCHED_IDLE are Linux-specific.
113
115 POSIX.1-2001.
116
118 Further details of the semantics of all of the above "normal" and
119 "real-time" scheduling policies can be found in the sched(7) manual
120 page. That page also describes an additional policy, SCHED_DEADLINE,
121 which is settable only via sched_setattr(2).
122
123 POSIX systems on which sched_setscheduler() and sched_getscheduler()
124 are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.
125
127 POSIX.1 says that on success, sched_setscheduler() should return the
128 previous scheduling policy. Linux sched_setscheduler() does not con‐
129 form to this requirement, since it always returns 0 on success.
130
132 chrt(1), nice(2), sched_get_priority_max(2), sched_get_priority_min(2),
133 sched_getaffinity(2), sched_getattr(2), sched_getparam(2),
134 sched_rr_get_interval(2), sched_setaffinity(2), sched_setattr(2),
135 sched_setparam(2), sched_yield(2), setpriority(2), capabilities(7),
136 cpuset(7), sched(7)
137
138
139
140Linux man-pages 6.05 2023-03-30 sched_setscheduler(2)