1SETSCHEDULER(2) Linux Programmer's Manual SETSCHEDULER(2)
2
3
4
6 sched_setscheduler, sched_getscheduler - set and get scheduling algo‐
7 rithm/parameters
8
10 #include <sched.h>
11
12 int sched_setscheduler(pid_t pid, int policy,
13 const struct sched_param *param);
14
15 int sched_getscheduler(pid_t pid);
16
17 struct sched_param {
18 ...
19 int sched_priority;
20 ...
21 };
22
24 sched_setscheduler() sets both the scheduling policy and the associated
25 parameters for the process identified by pid. If pid equals zero, the
26 scheduler of the calling process will be set. The interpretation of the
27 parameter param depends on the selected policy. Currently, the follow‐
28 ing three scheduling policies are supported under Linux: SCHED_FIFO,
29 SCHED_RR, SCHED_OTHER, and SCHED_BATCH; their respective semantics are
30 described below.
31
32 sched_getscheduler() queries the scheduling policy currently applied to
33 the process identified by pid. If pid equals zero, the policy of the
34 calling process will be retrieved.
35
36 Scheduling Policies
37 The scheduler is the kernel part that decides which runnable process
38 will be executed by the CPU next. The Linux scheduler offers three dif‐
39 ferent scheduling policies, one for normal processes and two for real-
40 time applications. A static priority value sched_priority is assigned
41 to each process and this value can be changed only via system calls.
42 Conceptually, the scheduler maintains a list of runnable processes for
43 each possible sched_priority value, and sched_priority can have a value
44 in the range 0 to 99. In order to determine the process that runs next,
45 the Linux scheduler looks for the non-empty list with the highest
46 static priority and takes the process at the head of this list. The
47 scheduling policy determines for each process, where it will be
48 inserted into the list of processes with equal static priority and how
49 it will move inside this list.
50
51 SCHED_OTHER is the default universal time-sharing scheduler policy used
52 by most processes. SCHED_BATCH is intended for "batch" style execution
53 of processes. SCHED_FIFO and SCHED_RR are intended for special time-
54 critical applications that need precise control over the way in which
55 runnable processes are selected for execution.
56
57 Processes scheduled with SCHED_OTHER or SCHED_BATCH must be assigned
58 the static priority 0. Processes scheduled under SCHED_FIFO or
59 SCHED_RR can have a static priority in the range 1 to 99. The system
60 calls sched_get_priority_min() and sched_get_priority_max() can be used
61 to find out the valid priority range for a scheduling policy in a por‐
62 table way on all POSIX.1-2001 conforming systems.
63
64 All scheduling is preemptive: If a process with a higher static prior‐
65 ity gets ready to run, the current process will be preempted and
66 returned into its wait list. The scheduling policy only determines the
67 ordering within the list of runnable processes with equal static prior‐
68 ity.
69
70 SCHED_FIFO: First In-First Out scheduling
71 SCHED_FIFO can only be used with static priorities higher than 0, which
72 means that when a SCHED_FIFO processes becomes runnable, it will always
73 immediately preempt any currently running SCHED_OTHER or SCHED_BATCH
74 process. SCHED_FIFO is a simple scheduling algorithm without time
75 slicing. For processes scheduled under the SCHED_FIFO policy, the fol‐
76 lowing rules are applied: A SCHED_FIFO process that has been preempted
77 by another process of higher priority will stay at the head of the list
78 for its priority and will resume execution as soon as all processes of
79 higher priority are blocked again. When a SCHED_FIFO process becomes
80 runnable, it will be inserted at the end of the list for its priority.
81 A call to sched_setscheduler() or sched_setparam() will put the
82 SCHED_FIFO (or SCHED_RR) process identified by pid at the start of the
83 list if it was runnable. As a consequence, it may preempt the cur‐
84 rently running process if it has the same priority. (POSIX.1-2001
85 specifies that the process should go to the end of the list.) A
86 process calling sched_yield() will be put at the end of the list. No
87 other events will move a process scheduled under the SCHED_FIFO policy
88 in the wait list of runnable processes with equal static priority. A
89 SCHED_FIFO process runs until either it is blocked by an I/O request,
90 it is preempted by a higher priority process, or it calls
91 sched_yield().
92
93 SCHED_RR: Round Robin scheduling
94 SCHED_RR is a simple enhancement of SCHED_FIFO. Everything described
95 above for SCHED_FIFO also applies to SCHED_RR, except that each process
96 is only allowed to run for a maximum time quantum. If a SCHED_RR
97 process has been running for a time period equal to or longer than the
98 time quantum, it will be put at the end of the list for its priority. A
99 SCHED_RR process that has been preempted by a higher priority process
100 and subsequently resumes execution as a running process will complete
101 the unexpired portion of its round robin time quantum. The length of
102 the time quantum can be retrieved using sched_rr_get_interval(2).
103
104 SCHED_OTHER: Default Linux time-sharing scheduling
105 SCHED_OTHER can only be used at static priority 0. SCHED_OTHER is the
106 standard Linux time-sharing scheduler that is intended for all pro‐
107 cesses that do not require special static priority real-time mecha‐
108 nisms. The process to run is chosen from the static priority 0 list
109 based on a dynamic priority that is determined only inside this list.
110 The dynamic priority is based on the nice level (set by nice(2) or set‐
111 priority(2)) and increased for each time quantum the process is ready
112 to run, but denied to run by the scheduler. This ensures fair progress
113 among all SCHED_OTHER processes.
114
115 SCHED_BATCH: Scheduling batch processes
116 (Since Linux 2.6.16.) SCHED_BATCH can only be used at static priority
117 0. This policy is similar to SCHED_OTHER, except that this policy will
118 cause the scheduler to always assume that the process is CPU-intensive.
119 Consequently, the scheduler will apply a small scheduling penalty so
120 that this process is mildly disfavoured in scheduling decisions. This
121 policy is useful for workloads that are non-interactive, but do not
122 want to lower their nice value, and for workloads that want a determin‐
123 istic scheduling policy without interactivity causing extra preemptions
124 (between the workload's tasks).
125
126 Privileges and resource limits
127 In Linux kernels before 2.6.12, only privileged (CAP_SYS_NICE) pro‐
128 cesses can set a non-zero static priority. The only change that an
129 unprivileged process can make is to set the SCHED_OTHER policy, and
130 this can only be done if the effective user ID of the caller of
131 sched_setscheduler() matches the real or effective user ID of the tar‐
132 get process (i.e., the process specified by pid) whose policy is being
133 changed.
134
135 Since Linux 2.6.12, the RLIMIT_RTPRIO resource limit defines a ceiling
136 on an unprivileged process's priority for the SCHED_RR and SCHED_FIFO
137 policies. If an unprivileged process has a non-zero RLIMIT_RTPRIO soft
138 limit, then it can change its scheduling policy and priority, subject
139 to the restriction that the priority cannot be set to a value higher
140 than the RLIMIT_RTPRIO soft limit. If the RLIMIT_RTPRIO soft limit is
141 0, then the only permitted change is to lower the priority. Subject to
142 the same rules, another unprivileged process can also make these
143 changes, as long as the effective user ID of the process making the
144 change matches the real or effective user ID of the target process.
145 See getrlimit(2) for further information on RLIMIT_RTPRIO. Privileged
146 (CAP_SYS_NICE) processes ignore this limit; as with older kernels, they
147 can make arbitrary changes to scheduling policy and priority.
148
149 Response time
150 A blocked high priority process waiting for the I/O has a certain
151 response time before it is scheduled again. The device driver writer
152 can greatly reduce this response time by using a "slow interrupt"
153 interrupt handler.
154
155 Miscellaneous
156 Child processes inherit the scheduling algorithm and parameters across
157 a fork(). The scheduling algorithm and parameters are preserved across
158 execve(2).
159
160 Memory locking is usually needed for real-time processes to avoid pag‐
161 ing delays, this can be done with mlock() or mlockall().
162
163 As a non-blocking end-less loop in a process scheduled under SCHED_FIFO
164 or SCHED_RR will block all processes with lower priority forever, a
165 software developer should always keep available on the console a shell
166 scheduled under a higher static priority than the tested application.
167 This will allow an emergency kill of tested real-time applications that
168 do not block or terminate as expected.
169
170 POSIX systems on which sched_setscheduler() and sched_getscheduler()
171 are available define _POSIX_PRIORITY_SCHEDULING in <unistd.h>.
172
174 On success, sched_setscheduler() returns zero. On success,
175 sched_getscheduler() returns the policy for the process (a non-negative
176 integer). On error, -1 is returned, and errno is set appropriately.
177
179 EINVAL The scheduling policy is not one of the recognized policies, or
180 the parameter param does not make sense for the policy.
181
182 EPERM The calling process does not have appropriate privileges.
183
184 ESRCH The process whose ID is pid could not be found.
185
187 POSIX.1-2001. The SCHED_BATCH policy is Linux specific.
188
190 Standard Linux is a general-purpose operating system and can handle
191 background processes, interactive applications, and soft real-time
192 applications (applications that need to usually meet timing deadlines).
193 This man page is directed at these kinds of applications.
194
195 Standard Linux is not designed to support hard real-time applications,
196 that is, applications in which deadlines (often much shorter than a
197 second) must be guaranteed or the system will fail catastrophically.
198 Like all general-purpose operating systems, Linux is designed to maxi‐
199 mize average case performance instead of worst case performance.
200 Linux's worst case performance for interrupt handling is much poorer
201 than its average case, its various kernel locks (such as for SMP) pro‐
202 duce long maximum wait times, and many of its performance improvement
203 techniques decrease average time by increasing worst-case time. For
204 most situations, that's what you want, but if you truly are developing
205 a hard real-time application, consider using hard real-time extensions
206 to Linux such as RTLinux (http://www.rtlinux.org) or RTAI
207 (http://www.rtai.org) or use a different operating system designed
208 specifically for hard real-time applications.
209
211 getpriority(2), mlock(2), mlockall(2), munlock(2), munlockall(2),
212 nice(2), sched_get_priority_max(2), sched_get_priority_min(2),
213 sched_getaffinity(2), sched_getparam(2), sched_rr_get_interval(2),
214 sched_setaffinity(2), sched_setparam(2), sched_yield(2), setprior‐
215 ity(2), capabilities(7)
216
217 Programming for the real world - POSIX.4 by Bill O. Gallmeister,
218 O'Reilly & Associates, Inc., ISBN 1-56592-074-0
219
220
221
222Linux 2.6.16 2006-03-23 SETSCHEDULER(2)