2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_setschedparam 3 (date) "Linux man-pages (unreleased)"
9 pthread_setschedparam, pthread_getschedparam \- set/get
10 scheduling policy and parameters of a thread
13 .RI ( libpthread ", " \-lpthread )
16 .B #include <pthread.h>
18 .BI "int pthread_setschedparam(pthread_t " thread ", int " policy ,
19 .BI " const struct sched_param *" param );
20 .BI "int pthread_getschedparam(pthread_t " thread ", int *restrict " policy ,
21 .BI " struct sched_param *restrict " param );
25 .BR pthread_setschedparam ()
26 function sets the scheduling policy and parameters of the thread
30 specifies the new scheduling policy for
32 The supported values for
34 and their semantics, are described in
36 .\" FIXME . pthread_setschedparam() places no restriction on the policy,
37 .\" but pthread_attr_setschedpolicy() restricts policy to RR/FIFO/OTHER
38 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7013
40 The structure pointed to by
42 specifies the new scheduling parameters for
44 Scheduling parameters are maintained in the following structure:
49 int sched_priority; /* Scheduling priority */
54 As can be seen, only one scheduling parameter is supported.
55 For details of the permitted ranges for scheduling priorities
56 in each scheduling policy, see
60 .BR pthread_getschedparam ()
61 function returns the scheduling policy and parameters of the thread
63 in the buffers pointed to by
68 The returned priority value is that set by the most recent
69 .BR pthread_setschedparam (),
70 .BR pthread_setschedprio (3),
72 .BR pthread_create (3)
75 The returned priority does not reflect any temporary priority adjustments
76 as a result of calls to any priority inheritance or
77 priority ceiling functions (see, for example,
78 .BR pthread_mutexattr_setprioceiling (3)
80 .BR pthread_mutexattr_setprotocol (3)).
81 .\" FIXME . nptl/pthread_setschedparam.c has the following
82 .\" /* If the thread should have higher priority because of some
83 .\" PTHREAD_PRIO_PROTECT mutexes it holds, adjust the priority. */
84 .\" Eventually (perhaps after writing the mutexattr pages), we
85 .\" may want to add something on the topic to this page.
87 On success, these functions return 0;
88 on error, they return a nonzero error number.
90 .BR pthread_setschedparam ()
91 fails, the scheduling policy and parameters of
95 Both of these functions can fail with the following error:
102 .BR pthread_setschedparam ()
103 may additionally fail with the following errors:
107 is not a recognized policy, or
109 does not make sense for the
113 The caller does not have appropriate privileges
114 to set the specified scheduling policy and parameters.
116 POSIX.1 also documents an
118 ("attempt was made to set the policy or scheduling parameters
119 to an unsupported value") error for
120 .BR pthread_setschedparam ().
122 For an explanation of the terms used in this section, see
128 Interface Attribute Value
132 .BR pthread_setschedparam (),
133 .BR pthread_getschedparam ()
134 T} Thread safety MT-Safe
142 For a description of the permissions required to, and the effect of,
143 changing a thread's scheduling policy and priority,
144 and details of the permitted ranges for priorities
145 in each scheduling policy, see
148 The program below demonstrates the use of
149 .BR pthread_setschedparam ()
151 .BR pthread_getschedparam (),
152 as well as the use of a number of other scheduling-related
155 In the following run, the main thread sets its scheduling policy to
157 with a priority of 10,
158 and initializes a thread attributes object with
159 a scheduling policy attribute of
161 and a scheduling priority attribute of 20.
162 The program then sets (using
163 .BR pthread_attr_setinheritsched (3))
164 the inherit scheduler attribute of the thread attributes object to
165 .BR PTHREAD_EXPLICIT_SCHED ,
166 meaning that threads created using this attributes object should
167 take their scheduling attributes from the thread attributes object.
168 The program then creates a thread using the thread attributes object,
169 and that thread displays its scheduling policy and priority.
173 $ \fBsu\fP # Need privilege to set real\-time scheduling policies
175 # \fB./a.out \-mf10 \-ar20 \-i e\fP
176 Scheduler settings of main thread
177 policy=SCHED_FIFO, priority=10
179 Scheduler settings in \[aq]attr\[aq]
180 policy=SCHED_RR, priority=20
181 inheritsched is EXPLICIT
183 Scheduler attributes of new thread
184 policy=SCHED_RR, priority=20
188 In the above output, one can see that the scheduling policy and priority
189 were taken from the values specified in the thread attributes object.
191 The next run is the same as the previous,
192 except that the inherit scheduler attribute is set to
193 .BR PTHREAD_INHERIT_SCHED ,
194 meaning that threads created using the thread attributes object should
195 ignore the scheduling attributes specified in the attributes object
196 and instead take their scheduling attributes from the creating thread.
200 # \fB./a.out \-mf10 \-ar20 \-i i\fP
201 Scheduler settings of main thread
202 policy=SCHED_FIFO, priority=10
204 Scheduler settings in \[aq]attr\[aq]
205 policy=SCHED_RR, priority=20
206 inheritsched is INHERIT
208 Scheduler attributes of new thread
209 policy=SCHED_FIFO, priority=10
213 In the above output, one can see that the scheduling policy and priority
214 were taken from the creating thread,
215 rather than the thread attributes object.
217 Note that if we had omitted the
219 option, the output would have been the same, since
220 .B PTHREAD_INHERIT_SCHED
221 is the default for the inherit scheduler attribute.
224 .\" SRC BEGIN (pthreads_sched_test.c)
226 /* pthreads_sched_test.c */
234 #define handle_error_en(en, msg) \e
235 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
239 usage(char *prog_name, char *msg)
244 fprintf(stderr, "Usage: %s [options]\en", prog_name);
245 fprintf(stderr, "Options are:\en");
246 #define fpe(msg) fprintf(stderr, "\et%s", msg) /* Shorter */
247 fpe("\-a<policy><prio> Set scheduling policy and priority in\en");
248 fpe(" thread attributes object\en");
249 fpe(" <policy> can be\en");
250 fpe(" f SCHED_FIFO\en");
251 fpe(" r SCHED_RR\en");
252 fpe(" o SCHED_OTHER\en");
253 fpe("\-A Use default thread attributes object\en");
254 fpe("\-i {e|i} Set inherit scheduler attribute to\en");
255 fpe(" \[aq]explicit\[aq] or \[aq]inherit\[aq]\en");
256 fpe("\-m<policy><prio> Set scheduling policy and priority on\en");
257 fpe(" main thread before pthread_create() call\en");
262 get_policy(char p, int *policy)
265 case \[aq]f\[aq]: *policy = SCHED_FIFO; return 1;
266 case \[aq]r\[aq]: *policy = SCHED_RR; return 1;
267 case \[aq]o\[aq]: *policy = SCHED_OTHER; return 1;
273 display_sched_attr(int policy, const struct sched_param *param)
275 printf(" policy=%s, priority=%d\en",
276 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
277 (policy == SCHED_RR) ? "SCHED_RR" :
278 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
280 param\->sched_priority);
284 display_thread_sched_attr(char *msg)
287 struct sched_param param;
289 s = pthread_getschedparam(pthread_self(), &policy, ¶m);
291 handle_error_en(s, "pthread_getschedparam");
293 printf("%s\en", msg);
294 display_sched_attr(policy, ¶m);
298 thread_start(void *arg)
300 display_thread_sched_attr("Scheduler attributes of new thread");
306 main(int argc, char *argv[])
308 int s, opt, inheritsched, use_null_attrib, policy;
311 pthread_attr_t *attrp;
312 char *attr_sched_str, *main_sched_str, *inheritsched_str;
313 struct sched_param param;
315 /* Process command\-line options. */
318 attr_sched_str = NULL;
319 main_sched_str = NULL;
320 inheritsched_str = NULL;
322 while ((opt = getopt(argc, argv, "a:Ai:m:")) != \-1) {
324 case \[aq]a\[aq]: attr_sched_str = optarg; break;
325 case \[aq]A\[aq]: use_null_attrib = 1; break;
326 case \[aq]i\[aq]: inheritsched_str = optarg; break;
327 case \[aq]m\[aq]: main_sched_str = optarg; break;
328 default: usage(argv[0], "Unrecognized option\en");
333 && (inheritsched_str != NULL || attr_sched_str != NULL))
335 usage(argv[0], "Can\[aq]t specify \-A with \-i or \-a\en");
338 /* Optionally set scheduling attributes of main thread,
339 and display the attributes. */
341 if (main_sched_str != NULL) {
342 if (!get_policy(main_sched_str[0], &policy))
343 usage(argv[0], "Bad policy for main thread (\-m)\en");
344 param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
346 s = pthread_setschedparam(pthread_self(), policy, ¶m);
348 handle_error_en(s, "pthread_setschedparam");
351 display_thread_sched_attr("Scheduler settings of main thread");
354 /* Initialize thread attributes object according to options. */
358 if (!use_null_attrib) {
359 s = pthread_attr_init(&attr);
361 handle_error_en(s, "pthread_attr_init");
365 if (inheritsched_str != NULL) {
366 if (inheritsched_str[0] == \[aq]e\[aq])
367 inheritsched = PTHREAD_EXPLICIT_SCHED;
368 else if (inheritsched_str[0] == \[aq]i\[aq])
369 inheritsched = PTHREAD_INHERIT_SCHED;
371 usage(argv[0], "Value for \-i must be \[aq]e\[aq] or \[aq]i\[aq]\en");
373 s = pthread_attr_setinheritsched(&attr, inheritsched);
375 handle_error_en(s, "pthread_attr_setinheritsched");
378 if (attr_sched_str != NULL) {
379 if (!get_policy(attr_sched_str[0], &policy))
380 usage(argv[0], "Bad policy for \[aq]attr\[aq] (\-a)\en");
381 param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
383 s = pthread_attr_setschedpolicy(&attr, policy);
385 handle_error_en(s, "pthread_attr_setschedpolicy");
386 s = pthread_attr_setschedparam(&attr, ¶m);
388 handle_error_en(s, "pthread_attr_setschedparam");
391 /* If we initialized a thread attributes object, display
392 the scheduling attributes that were set in the object. */
395 s = pthread_attr_getschedparam(&attr, ¶m);
397 handle_error_en(s, "pthread_attr_getschedparam");
398 s = pthread_attr_getschedpolicy(&attr, &policy);
400 handle_error_en(s, "pthread_attr_getschedpolicy");
402 printf("Scheduler settings in \[aq]attr\[aq]\en");
403 display_sched_attr(policy, ¶m);
405 pthread_attr_getinheritsched(&attr, &inheritsched);
406 printf(" inheritsched is %s\en",
407 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
408 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
413 /* Create a thread that will display its scheduling attributes. */
415 s = pthread_create(&thread, attrp, &thread_start, NULL);
417 handle_error_en(s, "pthread_create");
419 /* Destroy unneeded thread attributes object. */
421 if (!use_null_attrib) {
422 s = pthread_attr_destroy(&attr);
424 handle_error_en(s, "pthread_attr_destroy");
427 s = pthread_join(thread, NULL);
429 handle_error_en(s, "pthread_join");
439 .BR sched_get_priority_min (2),
440 .BR pthread_attr_init (3),
441 .BR pthread_attr_setinheritsched (3),
442 .BR pthread_attr_setschedparam (3),
443 .BR pthread_attr_setschedpolicy (3),
444 .BR pthread_create (3),
445 .BR pthread_self (3),
446 .BR pthread_setschedprio (3),