1PTHREAD_SETSCHEDPARAM(3)   Linux Programmer's Manual  PTHREAD_SETSCHEDPARAM(3)
2
3
4

NAME

6       pthread_setschedparam,  pthread_getschedparam - set/get scheduling pol‐
7       icy and parameters of a thread
8

SYNOPSIS

10       #include <pthread.h>
11
12       pthread_setschedparam(pthread_t thread, int policy,
13                             const struct sched_param *param);
14       pthread_getschedparam(pthread_t thread, int *policy,
15                             struct sched_param *param);
16
17       Compile and link with -pthread.
18

DESCRIPTION

20       The pthread_setschedparam() function sets  the  scheduling  policy  and
21       parameters of the thread thread.
22
23       policy  specifies  the new scheduling policy for thread.  The supported
24       values  for   policy,   and   their   semantics,   are   described   in
25       sched_setscheduler(2).
26
27       The  structure pointed to by param specifies the new scheduling parame‐
28       ters for thread.  Scheduling parameters are maintained in the following
29       structure:
30
31           struct sched_param {
32               int sched_priority;     /* Scheduling priority */
33           };
34
35       As  can  be  seen,  only  one  scheduling  parameter is supported.  For
36       details of the permitted  ranges  for  scheduling  priorities  in  each
37       scheduling policy, see sched_setscheduler(2).
38
39       The  pthread_getschedparam() function returns the scheduling policy and
40       parameters of the thread thread, in the buffers pointed  to  by  policy
41       and  param,  respectively.   The returned priority value is that set by
42       the most recent  pthread_setschedparam(),  pthread_setschedprio(3),  or
43       pthread_create(3)  call  that  affected  thread.  The returned priority
44       does not reflect any temporary priority  adjustments  as  a  result  of
45       calls  to  any priority inheritance or priority ceiling functions (see,
46       for  example,  pthread_mutexattr_setprioceiling(3)  and  pthread_mutex‐
47       attr_setprotocol(3)).
48

RETURN VALUE

50       On  success,  these functions return 0; on error, they return a nonzero
51       error number.  If pthread_setschedparam() fails, the scheduling  policy
52       and parameters of thread are not changed.
53

ERRORS

55       Both of these functions can fail with the following error:
56
57       ESRCH  No thread with the ID thread could be found.
58
59       pthread_setschedparam()   may  additionally  fail  with  the  following
60       errors:
61
62       EINVAL policy is not a recognized policy, or param does not make  sense
63              for the policy.
64
65       EPERM  The caller does not have appropriate privileges to set the spec‐
66              ified scheduling policy and parameters.
67
68       POSIX.1-2001 also documents an ENOTSUP ("attempt was made  to  set  the
69       policy  or  scheduling  parameters  to an unsupported value") error for
70       pthread_setschedparam().
71

CONFORMING TO

73       POSIX.1-2001.
74

NOTES

76       For a description of the permissions required to, and  the  effect  of,
77       changing  a thread's scheduling policy and priority, and details of the
78       permitted  ranges  for  priorities  in  each  scheduling  policy,   see
79       sched_setscheduler(2).
80

EXAMPLE

82       The  program  below demonstrates the use of pthread_setschedparam() and
83       pthread_getschedparam(), as well as the use of a number of other sched‐
84       uling-related pthreads functions.
85
86       In  the  following  run,  the main thread sets its scheduling policy to
87       SCHED_FIFO with a priority of 10, and initializes a  thread  attributes
88       object  with a scheduling policy attribute of SCHED_RR and a scheduling
89       priority   attribute   of   20.    The   program   then   sets   (using
90       pthread_attr_setinheritsched(3)) the inherit scheduler attribute of the
91       thread  attributes  object  to  PTHREAD_EXPLICIT_SCHED,  meaning   that
92       threads created using this attributes object should take their schedul‐
93       ing attributes from the thread attributes  object.   The  program  then
94       creates  a  thread  using the thread attributes object, and that thread
95       displays its scheduling policy and priority.
96
97           $ su      # Need privilege to set real-time scheduling policies
98           Password:
99           # ./a.out -mf10 -ar20 -i e
100           Scheduler settings of main thread
101               policy=SCHED_FIFO, priority=10
102
103           Scheduler settings in 'attr'
104               policy=SCHED_RR, priority=20
105               inheritsched is EXPLICIT
106
107           Scheduler attributes of new thread
108               policy=SCHED_RR, priority=20
109
110       In the above output, one can see that the scheduling policy and  prior‐
111       ity  were  taken  from  the  values  specified in the thread attributes
112       object.
113
114       The next run is the same as  the  previous,  except  that  the  inherit
115       scheduler  attribute  is  set  to  PTHREAD_INHERIT_SCHED,  meaning that
116       threads created using the thread attributes object  should  ignore  the
117       scheduling  attributes  specified  in the attributes object and instead
118       take their scheduling attributes from the creating thread.
119
120           # ./a.out -mf10 -ar20 -i i
121           Scheduler settings of main thread
122               policy=SCHED_FIFO, priority=10
123
124           Scheduler settings in 'attr'
125               policy=SCHED_RR, priority=20
126               inheritsched is INHERIT
127
128           Scheduler attributes of new thread
129               policy=SCHED_FIFO, priority=10
130
131       In the above output, one can see that the scheduling policy and  prior‐
132       ity  were  taken  from  the  creating  thread,  rather  than the thread
133       attributes object.
134
135       Note that if we had omitted the -i i option, the output would have been
136       the  same,  since  PTHREAD_INHERIT_SCHED is the default for the inherit
137       scheduler attribute.
138
139   Program source
140
141       /* pthreads_sched_test.c */
142
143       #include <pthread.h>
144       #include <stdio.h>
145       #include <stdlib.h>
146       #include <unistd.h>
147       #include <errno.h>
148
149       #define handle_error_en(en, msg) \
150               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
151
152       static void
153       usage(char *prog_name, char *msg)
154       {
155           if (msg != NULL)
156               fputs(msg, stderr);
157
158           fprintf(stderr, "Usage: %s [options]\n", prog_name);
159           fprintf(stderr, "Options are:\n");
160       #define fpe(msg) fprintf(stderr, "\t%s", msg);          /* Shorter */
161           fpe("-a<policy><prio> Set scheduling policy and priority in\n");
162           fpe("                 thread attributes object\n");
163           fpe("                 <policy> can be\n");
164           fpe("                     f  SCHED_FIFO\n");
165           fpe("                     r  SCHED_RR\n");
166           fpe("                     o  SCHED_OTHER\n");
167           fpe("-A               Use default thread attributes object\n");
168           fpe("-i {e|s}         Set inherit scheduler attribute to\n");
169           fpe("                 'explicit' or 'inherit'\n");
170           fpe("-m<policy><prio> Set scheduling policy and priority on\n");
171           fpe("                 main thread before pthread_create() call\n");
172           exit(EXIT_FAILURE);
173       }
174
175       static int
176       get_policy(char p, int *policy)
177       {
178           switch (p) {
179           case 'f': *policy = SCHED_FIFO;     return 1;
180           case 'r': *policy = SCHED_RR;       return 1;
181           case 'o': *policy = SCHED_OTHER;    return 1;
182           default:  return 0;
183           }
184       }
185
186       static void
187       display_sched_attr(int policy, struct sched_param *param)
188       {
189           printf("    policy=%s, priority=%d\n",
190                   (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
191                   (policy == SCHED_RR)    ? "SCHED_RR" :
192                   (policy == SCHED_OTHER) ? "SCHED_OTHER" :
193                   "???",
194                   param->sched_priority);
195       }
196
197       static void
198       display_thread_sched_attr(char *msg)
199       {
200           int policy, s;
201           struct sched_param param;
202
203           s = pthread_getschedparam(pthread_self(), &policy, &param);
204           if (s != 0)
205               handle_error_en(s, "pthread_getschedparam");
206
207           printf("%s\n", msg);
208           display_sched_attr(policy, &param);
209       }
210
211       static void *
212       thread_start(void *arg)
213       {
214           display_thread_sched_attr("Scheduler attributes of new thread");
215
216           return NULL;
217       }
218
219       int
220       main(int argc, char *argv[])
221       {
222           int s, opt, inheritsched, use_null_attrib, policy;
223           pthread_t thread;
224           pthread_attr_t attr;
225           pthread_attr_t *attrp;
226           char *attr_sched_str, *main_sched_str, *inheritsched_str;
227           struct sched_param param;
228
229           /* Process command-line options */
230
231           use_null_attrib = 0;
232           attr_sched_str = NULL;
233           main_sched_str = NULL;
234           inheritsched_str = NULL;
235
236           while ((opt = getopt(argc, argv, "a:Ai:m:")) != -1) {
237               switch (opt) {
238               case 'a': attr_sched_str = optarg;      break;
239               case 'A': use_null_attrib = 1;          break;
240               case 'i': inheritsched_str = optarg;    break;
241               case 'm': main_sched_str = optarg;      break;
242               default:  usage(argv[0], "Unrecognized option\n");
243               }
244           }
245
246           if (use_null_attrib &&
247                   (inheritsched_str != NULL || attr_sched_str != NULL))
248               usage(argv[0], "Can't specify -A with -i or -a\n");
249
250           /* Optionally set scheduling attributes of main thread,
251              and display the attributes */
252
253           if (main_sched_str != NULL) {
254               if (!get_policy(main_sched_str[0], &policy))
255                   usage(argv[0], "Bad policy for main thread (-s)\n");
256               param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
257
258               s = pthread_setschedparam(pthread_self(), policy, &param);
259               if (s != 0)
260                   handle_error_en(s, "pthread_setschedparam");
261           }
262
263           display_thread_sched_attr("Scheduler settings of main thread");
264           printf("\n");
265
266           /* Initialize thread attributes object according to options */
267
268           attrp = NULL;
269
270           if (!use_null_attrib) {
271               s = pthread_attr_init(&attr);
272               if (s != 0)
273                   handle_error_en(s, "pthread_attr_init");
274               attrp = &attr;
275           }
276
277           if (inheritsched_str != NULL) {
278               if (inheritsched_str[0] == 'e')
279                   inheritsched = PTHREAD_EXPLICIT_SCHED;
280               else if (inheritsched_str[0] == 'i')
281                   inheritsched = PTHREAD_INHERIT_SCHED;
282               else
283                   usage(argv[0], "Value for -i must be 'e' or 'i'\n");
284
285               s = pthread_attr_setinheritsched(&attr, inheritsched);
286               if (s != 0)
287                   handle_error_en(s, "pthread_attr_setinheritsched");
288           }
289
290           if (attr_sched_str != NULL) {
291               if (!get_policy(attr_sched_str[0], &policy))
292                   usage(argv[0],
293                           "Bad policy for 'attr' (-a)\n");
294               param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
295
296               s = pthread_attr_setschedpolicy(&attr, policy);
297               if (s != 0)
298                   handle_error_en(s, "pthread_attr_setschedpolicy");
299               s = pthread_attr_setschedparam(&attr, &param);
300               if (s != 0)
301                   handle_error_en(s, "pthread_attr_setschedparam");
302           }
303
304           /* If we initialized a thread attributes object, display
305              the scheduling attributes that were set in the object */
306
307           if (attrp != NULL) {
308               s = pthread_attr_getschedparam(&attr, &param);
309               if (s != 0)
310                   handle_error_en(s, "pthread_attr_getschedparam");
311               s = pthread_attr_getschedpolicy(&attr, &policy);
312               if (s != 0)
313                   handle_error_en(s, "pthread_attr_getschedpolicy");
314
315               printf("Scheduler settings in 'attr'\n");
316               display_sched_attr(policy, &param);
317
318               s = pthread_attr_getinheritsched(&attr, &inheritsched);
319               printf("    inheritsched is %s\n",
320                       (inheritsched == PTHREAD_INHERIT_SCHED)  ? "INHERIT" :
321                       (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
322                       "???");
323               printf("\n");
324           }
325
326           /* Create a thread that will display its scheduling attributes */
327
328           s = pthread_create(&thread, attrp, &thread_start, NULL);
329           if (s != 0)
330               handle_error_en(s, "pthread_create");
331
332           /* Destroy unneeded thread attributes object */
333
334           s = pthread_attr_destroy(&attr);
335           if (s != 0)
336               handle_error_en(s, "pthread_attr_destroy");
337
338           s = pthread_join(thread, NULL);
339           if (s != 0)
340               handle_error_en(s, "pthread_join");
341
342           exit(EXIT_SUCCESS);
343       }
344

SEE ALSO

346       getrlimit(2), sched_get_priority_min(2), sched_setscheduler(2),
347       pthread_attr_init(3), pthread_attr_setinheritsched(3),
348       pthread_attr_setschedparam(3), pthread_attr_setschedpolicy(3),
349       pthread_create(3), pthread_self(3), pthread_setschedprio(3),
350       pthreads(7)
351

COLOPHON

353       This page is part of release 3.53 of the Linux man-pages project.  A
354       description of the project, and information about reporting bugs, can
355       be found at http://www.kernel.org/doc/man-pages/.
356
357
358
359Linux                             2008-11-17          PTHREAD_SETSCHEDPARAM(3)
Impressum