OSDN Git Service

(split) LDP: Update original to LDP v3.65
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_setschedparam.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH PTHREAD_SETSCHEDPARAM 3 2008-11-17 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_setschedparam, pthread_getschedparam \- set/get
29 scheduling policy and parameters of a thread
30 .SH SYNOPSIS
31 .nf
32 .B #include <pthread.h>
33
34 .BI "pthread_setschedparam(pthread_t " thread ", int " policy ,
35 .BI "                      const struct sched_param *" param );
36 .BI "pthread_getschedparam(pthread_t " thread ", int *" policy ,
37 .BI "                      struct sched_param *" param );
38 .sp
39 Compile and link with \fI\-pthread\fP.
40 .fi
41 .SH DESCRIPTION
42 The
43 .BR pthread_setschedparam ()
44 function sets the scheduling policy and parameters of the thread
45 .IR thread .
46
47 .I policy
48 specifies the new scheduling policy for
49 .IR thread .
50 The supported values for
51 .IR policy ,
52 and their semantics, are described in
53 .BR sched_setscheduler (2).
54 .\" FIXME . pthread_setschedparam() places no restriction on the policy,
55 .\" but pthread_attr_setschedpolicy() restricts policy to RR/FIFO/OTHER
56 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7013
57
58 The structure pointed to by
59 .I param
60 specifies the new scheduling parameters for
61 .IR thread .
62 Scheduling parameters are maintained in the following structure:
63
64 .in +4n
65 .nf
66 struct sched_param {
67     int sched_priority;     /* Scheduling priority */
68 };
69 .fi
70 .in
71
72 As can be seen, only one scheduling parameter is supported.
73 For details of the permitted ranges for scheduling priorities
74 in each scheduling policy, see
75 .BR sched_setscheduler (2).
76
77 The
78 .BR pthread_getschedparam ()
79 function returns the scheduling policy and parameters of the thread
80 .IR thread ,
81 in the buffers pointed to by
82 .I policy
83 and
84 .IR param ,
85 respectively.
86 The returned priority value is that set by the most recent
87 .BR pthread_setschedparam (),
88 .BR pthread_setschedprio (3),
89 or
90 .BR pthread_create (3)
91 call that affected
92 .IR thread .
93 The returned priority does not reflect any temporary priority adjustments
94 as a result of calls to any priority inheritance or
95 priority ceiling functions (see, for example,
96 .BR pthread_mutexattr_setprioceiling (3)
97 and
98 .BR pthread_mutexattr_setprotocol (3)).
99 .\" FIXME . nptl/pthread_setschedparam.c has the following
100 .\"   /* If the thread should have higher priority because of some
101 .\"      PTHREAD_PRIO_PROTECT mutexes it holds, adjust the priority. */
102 .\" Eventually (perhaps after writing the mutexattr pages), we
103 .\" may want to add something on the topic to this page.
104 .SH RETURN VALUE
105 On success, these functions return 0;
106 on error, they return a nonzero error number.
107 If
108 .BR pthread_setschedparam ()
109 fails, the scheduling policy and parameters of
110 .I thread
111 are not changed.
112 .SH ERRORS
113 Both of these functions can fail with the following error:
114 .TP
115 .B ESRCH
116 No thread with the ID
117 .I thread
118 could be found.
119 .PP
120 .BR pthread_setschedparam ()
121 may additionally fail with the following errors:
122 .TP
123 .B EINVAL
124 .I policy
125 is not a recognized policy, or
126 .I param
127 does not make sense for the
128 .IR policy .
129 .TP
130 .B EPERM
131 The caller does not have appropriate privileges
132 to set the specified scheduling policy and parameters.
133 .PP
134 POSIX.1-2001 also documents an
135 .B ENOTSUP
136 ("attempt was made to set the policy or scheduling parameters
137 to an unsupported value") error for
138 .BR pthread_setschedparam ().
139 .\" .SH VERSIONS
140 .\" Available since glibc 2.0
141 .SH CONFORMING TO
142 POSIX.1-2001.
143 .SH NOTES
144 For a description of the permissions required to, and the effect of,
145 changing a thread's scheduling policy and priority,
146 and details of the permitted ranges for priorities
147 in each scheduling policy, see
148 .BR sched_setscheduler (2).
149 .SH EXAMPLE
150 The program below demonstrates the use of
151 .BR pthread_setschedparam ()
152 and
153 .BR pthread_getschedparam (),
154 as well as the use of a number of other scheduling-related
155 pthreads functions.
156
157 In the following run, the main thread sets its scheduling policy to
158 .BR SCHED_FIFO
159 with a priority of 10,
160 and initializes a thread attributes object with
161 a scheduling policy attribute of
162 .BR SCHED_RR
163 and a scheduling priority attribute of 20.
164 The program then sets (using
165 .BR pthread_attr_setinheritsched (3))
166 the inherit scheduler attribute of the thread attributes object to
167 .BR PTHREAD_EXPLICIT_SCHED ,
168 meaning that threads created using this attributes object should
169 take their scheduling attributes from the thread attributes object.
170 The program then creates a thread using the thread attributes object,
171 and that thread displays its scheduling policy and priority.
172 .in +4n
173 .nf
174
175 $ \fBsu\fP      # Need privilege to set real-time scheduling policies
176 Password:
177 # \fB./a.out \-mf10 \-ar20 \-i e\fP
178 Scheduler settings of main thread
179     policy=SCHED_FIFO, priority=10
180
181 Scheduler settings in \(aqattr\(aq
182     policy=SCHED_RR, priority=20
183     inheritsched is EXPLICIT
184
185 Scheduler attributes of new thread
186     policy=SCHED_RR, priority=20
187 .fi
188 .in
189
190 In the above output, one can see that the scheduling policy and priority
191 were taken from the values specified in the thread attributes object.
192
193 The next run is the same as the previous,
194 except that the inherit scheduler attribute is set to
195 .BR PTHREAD_INHERIT_SCHED ,
196 meaning that threads created using the thread attributes object should
197 ignore the scheduling attributes specified in the attributes object
198 and instead take their scheduling attributes from the creating thread.
199
200 .in +4n
201 .nf
202 # \fB./a.out \-mf10 \-ar20 \-i i\fP
203 Scheduler settings of main thread
204     policy=SCHED_FIFO, priority=10
205
206 Scheduler settings in \(aqattr\(aq
207     policy=SCHED_RR, priority=20
208     inheritsched is INHERIT
209
210 Scheduler attributes of new thread
211     policy=SCHED_FIFO, priority=10
212 .fi
213 .in
214
215 In the above output, one can see that the scheduling policy and priority
216 were taken from the creating thread,
217 rather than the thread attributes object.
218
219 Note that if we had omitted the
220 .IR "\-i\ i"
221 option, the output would have been the same, since
222 .BR PTHREAD_INHERIT_SCHED
223 is the default for the inherit scheduler attribute.
224 .SS Program source
225 \&
226 .nf
227 /* pthreads_sched_test.c */
228
229 #include <pthread.h>
230 #include <stdio.h>
231 #include <stdlib.h>
232 #include <unistd.h>
233 #include <errno.h>
234
235 #define handle_error_en(en, msg) \\
236         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
237
238 static void
239 usage(char *prog_name, char *msg)
240 {
241     if (msg != NULL)
242         fputs(msg, stderr);
243
244     fprintf(stderr, "Usage: %s [options]\\n", prog_name);
245     fprintf(stderr, "Options are:\\n");
246 #define fpe(msg) fprintf(stderr, "\\t%s", msg);          /* Shorter */
247     fpe("\-a<policy><prio> Set scheduling policy and priority in\\n");
248     fpe("                 thread attributes object\\n");
249     fpe("                 <policy> can be\\n");
250     fpe("                     f  SCHED_FIFO\\n");
251     fpe("                     r  SCHED_RR\\n");
252     fpe("                     o  SCHED_OTHER\\n");
253     fpe("\-A               Use default thread attributes object\\n");
254     fpe("\-i {e|s}         Set inherit scheduler attribute to\\n");
255     fpe("                 \(aqexplicit\(aq or \(aqinherit\(aq\\n");
256     fpe("\-m<policy><prio> Set scheduling policy and priority on\\n");
257     fpe("                 main thread before pthread_create() call\\n");
258     exit(EXIT_FAILURE);
259 }
260
261 static int
262 get_policy(char p, int *policy)
263 {
264     switch (p) {
265     case \(aqf\(aq: *policy = SCHED_FIFO;     return 1;
266     case \(aqr\(aq: *policy = SCHED_RR;       return 1;
267     case \(aqo\(aq: *policy = SCHED_OTHER;    return 1;
268     default:  return 0;
269     }
270 }
271
272 static void
273 display_sched_attr(int policy, struct sched_param *param)
274 {
275     printf("    policy=%s, priority=%d\\n",
276             (policy == SCHED_FIFO)  ? "SCHED_FIFO" :
277             (policy == SCHED_RR)    ? "SCHED_RR" :
278             (policy == SCHED_OTHER) ? "SCHED_OTHER" :
279             "???",
280             param\->sched_priority);
281 }
282
283 static void
284 display_thread_sched_attr(char *msg)
285 {
286     int policy, s;
287     struct sched_param param;
288
289     s = pthread_getschedparam(pthread_self(), &policy, &param);
290     if (s != 0)
291         handle_error_en(s, "pthread_getschedparam");
292
293     printf("%s\\n", msg);
294     display_sched_attr(policy, &param);
295 }
296
297 static void *
298 thread_start(void *arg)
299 {
300     display_thread_sched_attr("Scheduler attributes of new thread");
301
302     return NULL;
303 }
304
305 int
306 main(int argc, char *argv[])
307 {
308     int s, opt, inheritsched, use_null_attrib, policy;
309     pthread_t thread;
310     pthread_attr_t attr;
311     pthread_attr_t *attrp;
312     char *attr_sched_str, *main_sched_str, *inheritsched_str;
313     struct sched_param param;
314
315     /* Process command\-line options */
316
317     use_null_attrib = 0;
318     attr_sched_str = NULL;
319     main_sched_str = NULL;
320     inheritsched_str = NULL;
321
322     while ((opt = getopt(argc, argv, "a:Ai:m:")) != \-1) {
323         switch (opt) {
324         case \(aqa\(aq: attr_sched_str = optarg;      break;
325         case \(aqA\(aq: use_null_attrib = 1;          break;
326         case \(aqi\(aq: inheritsched_str = optarg;    break;
327         case \(aqm\(aq: main_sched_str = optarg;      break;
328         default:  usage(argv[0], "Unrecognized option\\n");
329         }
330     }
331
332     if (use_null_attrib &&
333             (inheritsched_str != NULL || attr_sched_str != NULL))
334         usage(argv[0], "Can\(aqt specify \-A with \-i or \-a\\n");
335
336     /* Optionally set scheduling attributes of main thread,
337        and display the attributes */
338
339     if (main_sched_str != NULL) {
340         if (!get_policy(main_sched_str[0], &policy))
341             usage(argv[0], "Bad policy for main thread (\-s)\\n");
342         param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
343
344         s = pthread_setschedparam(pthread_self(), policy, &param);
345         if (s != 0)
346             handle_error_en(s, "pthread_setschedparam");
347     }
348
349     display_thread_sched_attr("Scheduler settings of main thread");
350     printf("\\n");
351
352     /* Initialize thread attributes object according to options */
353
354     attrp = NULL;
355
356     if (!use_null_attrib) {
357         s = pthread_attr_init(&attr);
358         if (s != 0)
359             handle_error_en(s, "pthread_attr_init");
360         attrp = &attr;
361     }
362
363     if (inheritsched_str != NULL) {
364         if (inheritsched_str[0] == \(aqe\(aq)
365             inheritsched = PTHREAD_EXPLICIT_SCHED;
366         else if (inheritsched_str[0] == \(aqi\(aq)
367             inheritsched = PTHREAD_INHERIT_SCHED;
368         else
369             usage(argv[0], "Value for \-i must be \(aqe\(aq or \(aqi\(aq\\n");
370
371         s = pthread_attr_setinheritsched(&attr, inheritsched);
372         if (s != 0)
373             handle_error_en(s, "pthread_attr_setinheritsched");
374     }
375
376     if (attr_sched_str != NULL) {
377         if (!get_policy(attr_sched_str[0], &policy))
378             usage(argv[0],
379                     "Bad policy for \(aqattr\(aq (\-a)\\n");
380         param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
381
382         s = pthread_attr_setschedpolicy(&attr, policy);
383         if (s != 0)
384             handle_error_en(s, "pthread_attr_setschedpolicy");
385         s = pthread_attr_setschedparam(&attr, &param);
386         if (s != 0)
387             handle_error_en(s, "pthread_attr_setschedparam");
388     }
389
390     /* If we initialized a thread attributes object, display
391        the scheduling attributes that were set in the object */
392
393     if (attrp != NULL) {
394         s = pthread_attr_getschedparam(&attr, &param);
395         if (s != 0)
396             handle_error_en(s, "pthread_attr_getschedparam");
397         s = pthread_attr_getschedpolicy(&attr, &policy);
398         if (s != 0)
399             handle_error_en(s, "pthread_attr_getschedpolicy");
400
401         printf("Scheduler settings in \(aqattr\(aq\\n");
402         display_sched_attr(policy, &param);
403
404         s = pthread_attr_getinheritsched(&attr, &inheritsched);
405         printf("    inheritsched is %s\\n",
406                 (inheritsched == PTHREAD_INHERIT_SCHED)  ? "INHERIT" :
407                 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
408                 "???");
409         printf("\\n");
410     }
411
412     /* Create a thread that will display its scheduling attributes */
413
414     s = pthread_create(&thread, attrp, &thread_start, NULL);
415     if (s != 0)
416         handle_error_en(s, "pthread_create");
417
418     /* Destroy unneeded thread attributes object */
419
420     s = pthread_attr_destroy(&attr);
421     if (s != 0)
422         handle_error_en(s, "pthread_attr_destroy");
423
424     s = pthread_join(thread, NULL);
425     if (s != 0)
426         handle_error_en(s, "pthread_join");
427
428     exit(EXIT_SUCCESS);
429 }
430 .fi
431 .SH SEE ALSO
432 .ad l
433 .nh
434 .BR getrlimit (2),
435 .BR sched_get_priority_min (2),
436 .BR sched_setscheduler (2),
437 .BR pthread_attr_init (3),
438 .BR pthread_attr_setinheritsched (3),
439 .BR pthread_attr_setschedparam (3),
440 .BR pthread_attr_setschedpolicy (3),
441 .BR pthread_create (3),
442 .BR pthread_self (3),
443 .BR pthread_setschedprio (3),
444 .BR pthreads (7)
445 .SH COLOPHON
446 This page is part of release 3.65 of the Linux
447 .I man-pages
448 project.
449 A description of the project,
450 and information about reporting bugs,
451 can be found at
452 \%http://www.kernel.org/doc/man\-pages/.