1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"*******************************************************************
26 .\" This file was generated with po4a. Translate the source file.
28 .\"*******************************************************************
29 .TH PTHREAD_SETSCHEDPARAM 3 2008\-11\-17 Linux "Linux Programmer's Manual"
31 pthread_setschedparam, pthread_getschedparam \- スレッドの
32 スケジューリングポリシーとパラメータを設定/取得する
35 \fB#include <pthread.h>\fP
37 \fBpthread_setschedparam(pthread_t \fP\fIthread\fP\fB, int \fP\fIpolicy\fP\fB,\fP
38 \fB const struct sched_param *\fP\fIparam\fP\fB);\fP
39 \fBpthread_getschedparam(pthread_t \fP\fIthread\fP\fB, int *\fP\fIpolicy\fP\fB,\fP
40 \fB struct sched_param *\fP\fIparam\fP\fB);\fP
42 \fI\-pthread\fP でコンパイルしてリンクする。
45 \fBpthread_setschedparam\fP() 関数は、スレッド \fIthread\fP の
46 スケジューリングポリシーとスケジューリングパラメータを設定する。
48 .\" FIXME . pthread_setschedparam() places no restriction on the policy,
49 .\" but pthread_attr_setschedpolicy() restricts policy to RR/FIFO/OTHER
50 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7013
51 \fIpolicy\fP は \fIthread\fP の新しいスケジューリングポリシーを指定する。
52 \fIpolicy\fP に指定できる値とその意味は \fBsched_setscheduler\fP(2) で
55 \fIparam\fP が指す構造体は \fIthread\fP の新しいスケジューリングパラメータを
56 指定する。スケジューリングパラメータは以下の構造体で管理される。
61 int sched_priority; /* Scheduling priority */
66 見て分かる通り、サポートされているスケジューリングパラメータは一つだけ
67 である。各スケジューリングポリシーで許可されるスケジューリング優先度の
68 詳細については、\fBsched_setscheduler\fP(2) を参照のこと。
70 .\" FIXME . nptl/pthread_setschedparam.c has the following
71 .\" /* If the thread should have higher priority because of some
72 .\" PTHREAD_PRIO_PROTECT mutexes it holds, adjust the priority. */
73 .\" Eventually (perhaps after writing the mutexattr pages), we
74 .\" may want to add something on the topic to this page.
75 \fBpthread_getschedparam\fP() 関数は、スレッド \fIthread\fP の
77 それぞれ \fIpolicy\fP と \fIparam\fP が指すバッファに入れて返す。
78 返された優先度の値は、最も最近実行した \fIthread\fP に影響を与える
79 \fBpthread_setschedparam\fP(), \fBpthread_setschedprio\fP,
80 \fBpthread_create\fP で設定された値となる。
81 返された優先度は、優先度の継承や優先度の上限を設定する関数
82 (例えば \fBpthread_mutexattr_setprioceiling\fP(3) や
83 \fBpthread_mutexattr_setprotocol\fP(3) を参照) の呼び出しの結果
84 行われる一時的な優先度の調整の影響を受けない。
88 \fBpthread_setschedparam\fP() が失敗した場合、 \fIthread\fP の
89 スケジューリングポリシーとパラメータは変更されない。
91 これらの関数はどちらも以下のエラーで失敗する場合がある。
94 ID が \fIthread\fP のスレッドが見つからなかった。
96 \fBpthread_setschedparam\fP() はさらに以下のエラーで失敗する場合がある。
99 \fIpolicy\fP が認識できないポリシーであるか、
100 \fIparam\fP が \fIpolicy\fP では意味を持たない値である。
103 呼び出し側が、指定されたスケジューリングポリシーやパラメータを設定する
107 .\" Available since glibc 2.0
108 POSIX.1\-2001 では、 \fBpthread_setschedparam\fP() に関して
109 エラー \fBENOTSUP\fP ("サポートされていない値をスケジューリングポリシーや
110 パラメータに設定しようとした") も追加で規定されている。
114 スレッドのスケジューリングポリシーや優先度を変更するために必要な許可や
115 変更した場合の影響、および各スケジューリングポリシーで認められる優先度
116 の範囲の詳細については、 \fBsched_setscheduler\fP(2) を参照。
118 以下のプログラムは \fBpthread_setschedparam\fP() と
119 \fBpthread_getschedparam\fP() やスケジューリングに関連する pthreads の
120 他のいろいろな関数の使用例を示すものである。
122 以下の実行例では、メインスレッドは、自分のスケジューリングポリシーを
123 優先度 10 の \fBSCHED_FIFO\fP を設定し、スレッド属性オブジェクトを
124 スケジューリングポリシー属性 \fBSCHED_RR\fP とスケジューリング優先度
126 次に、このプログラムは (\fBpthread_attr_setinheritsched\fP(3) を使って)
127 そのスレッド属性オブジェクトの inherit scheduler 属性に
128 \fBPTHREAD_EXPLICIT_SCHED\fP を設定する。\fBPTHREAD_EXPLICIT_SCHED\fP は、
129 そのスレッド属性オブジェクトを使って作成されたスレッドはスレッド属性
130 オブジェクトからスケジューリング属性を取得して使うことを意味する。
131 それから、このスレッド属性オブジェクトを使ってスレッドを作成し、
132 作成したスレッドのスケジューリングポリシーと優先度を表示する。
136 $ \fBsu\fP # Need privilege to set real\-time scheduling policies
138 # \fB./a.out \-mf10 \-ar20 \-i e\fP
139 Scheduler settings of main thread
140 policy=SCHED_FIFO, priority=10
142 Scheduler settings in \(aqattr\(aq
143 policy=SCHED_RR, priority=20
144 inheritsched is EXPLICIT
146 Scheduler attributes of new thread
147 policy=SCHED_RR, priority=20
151 上記の出力では、スケジューリングポリシーと優先度がスレッド属性
152 オブジェクトで指定された値から取られていることが分かる。
154 次の実行例は前のものと同じだが、 inherit scheduler 属性が
155 \fBPTHREAD_INHERIT_SCHED\fP に設定される点が異なる。
156 \fBPTHREAD_INHERIT_SCHED\fP は、そのスレッド属性オブジェクトを使って作成
157 されたスレッドは、スレッド属性オブジェクトからスケジューリング属性を
158 無視し、代わりに呼び出したスレッドからスケジューリング属性を取得する
163 # \fB./a.out \-mf10 \-ar20 \-i i\fP
164 Scheduler settings of main thread
165 policy=SCHED_FIFO, priority=10
167 Scheduler settings in \(aqattr\(aq
168 policy=SCHED_RR, priority=20
169 inheritsched is INHERIT
171 Scheduler attributes of new thread
172 policy=SCHED_FIFO, priority=10
176 上記の出力では、スケジューリングポリシーと優先度が、
178 スレッドを作成したスレッドから取れれていることが分かる。
180 なお、 \fI\-i\ i\fP を省略した場合でも、
181 \fBPTHREAD_INHERIT_SCHED\fP が inherit scheduler 属性のデフォルト値なので、
186 /* pthreads_sched_test.c */
194 #define handle_error_en(en, msg) \e
195 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
198 usage(char *prog_name, char *msg)
203 fprintf(stderr, "Usage: %s [options]\en", prog_name);
204 fprintf(stderr, "Options are:\en");
205 #define fpe(msg) fprintf(stderr, "\et%s", msg); /* Shorter */
206 fpe("\-a<policy><prio> Set scheduling policy and priority in\en");
207 fpe(" thread attributes object\en");
208 fpe(" <policy> can be\en");
209 fpe(" f SCHED_FIFO\en");
210 fpe(" r SCHED_RR\en");
211 fpe(" o SCHED_OTHER\en");
212 fpe("\-A Use default thread attributes object\en");
213 fpe("\-i {e|s} Set inherit scheduler attribute to\en");
214 fpe(" \(aqexplicit\(aq or \(aqinherit\(aq\en");
215 fpe("\-m<policy><prio> Set scheduling policy and priority on\en");
216 fpe(" main thread before pthread_create() call\en");
221 get_policy(char p, int *policy)
224 case \(aqf\(aq: *policy = SCHED_FIFO; return 1;
225 case \(aqr\(aq: *policy = SCHED_RR; return 1;
226 case \(aqo\(aq: *policy = SCHED_OTHER; return 1;
232 display_sched_attr(int policy, struct sched_param *param)
234 printf(" policy=%s, priority=%d\en",
235 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
236 (policy == SCHED_RR) ? "SCHED_RR" :
237 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
239 param\->sched_priority);
243 display_thread_sched_attr(char *msg)
246 struct sched_param param;
248 s = pthread_getschedparam(pthread_self(), &policy, ¶m);
250 handle_error_en(s, "pthread_getschedparam");
252 printf("%s\en", msg);
253 display_sched_attr(policy, ¶m);
257 thread_start(void *arg)
259 display_thread_sched_attr("Scheduler attributes of new thread");
265 main(int argc, char *argv[])
267 int s, opt, inheritsched, use_null_attrib, policy;
270 pthread_attr_t *attrp;
271 char *attr_sched_str, *main_sched_str, *inheritsched_str;
272 struct sched_param param;
274 /* Process command\-line options */
277 attr_sched_str = NULL;
278 main_sched_str = NULL;
279 inheritsched_str = NULL;
281 while ((opt = getopt(argc, argv, "a:Ai:m:")) != \-1) {
283 case \(aqa\(aq: attr_sched_str = optarg; break;
284 case \(aqA\(aq: use_null_attrib = 1; break;
285 case \(aqi\(aq: inheritsched_str = optarg; break;
286 case \(aqm\(aq: main_sched_str = optarg; break;
287 default: usage(argv[0], "Unrecognized option\en");
291 if (use_null_attrib &&
292 (inheritsched_str != NULL || attr_sched_str != NULL))
293 usage(argv[0], "Can\(aqt specify \-A with \-i or \-a\en");
295 /* Optionally set scheduling attributes of main thread,
296 and display the attributes */
298 if (main_sched_str != NULL) {
299 if (!get_policy(main_sched_str[0], &policy))
300 usage(argv[0], "Bad policy for main thread (\-s)\en");
301 param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
303 s = pthread_setschedparam(pthread_self(), policy, ¶m);
305 handle_error_en(s, "pthread_setschedparam");
308 display_thread_sched_attr("Scheduler settings of main thread");
311 /* Initialize thread attributes object according to options */
315 if (!use_null_attrib) {
316 s = pthread_attr_init(&attr);
318 handle_error_en(s, "pthread_attr_init");
322 if (inheritsched_str != NULL) {
323 if (inheritsched_str[0] == \(aqe\(aq)
324 inheritsched = PTHREAD_EXPLICIT_SCHED;
325 else if (inheritsched_str[0] == \(aqi\(aq)
326 inheritsched = PTHREAD_INHERIT_SCHED;
328 usage(argv[0], "Value for \-i must be \(aqe\(aq or \(aqi\(aq\en");
330 s = pthread_attr_setinheritsched(&attr, inheritsched);
332 handle_error_en(s, "pthread_attr_setinheritsched");
335 if (attr_sched_str != NULL) {
336 if (!get_policy(attr_sched_str[0], &policy))
338 "Bad policy for \(aqattr\(aq (\-a)\en");
339 param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
341 s = pthread_attr_setschedpolicy(&attr, policy);
343 handle_error_en(s, "pthread_attr_setschedpolicy");
344 s = pthread_attr_setschedparam(&attr, ¶m);
346 handle_error_en(s, "pthread_attr_setschedparam");
349 /* If we initialized a thread attributes object, display
350 the scheduling attributes that were set in the object */
353 s = pthread_attr_getschedparam(&attr, ¶m);
355 handle_error_en(s, "pthread_attr_getschedparam");
356 s = pthread_attr_getschedpolicy(&attr, &policy);
358 handle_error_en(s, "pthread_attr_getschedpolicy");
360 printf("Scheduler settings in \(aqattr\(aq\en");
361 display_sched_attr(policy, ¶m);
363 s = pthread_attr_getinheritsched(&attr, &inheritsched);
364 printf(" inheritsched is %s\en",
365 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
366 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
371 /* Create a thread that will display its scheduling attributes */
373 s = pthread_create(&thread, attrp, &thread_start, NULL);
375 handle_error_en(s, "pthread_create");
377 /* Destroy unneeded thread attributes object */
379 s = pthread_attr_destroy(&attr);
381 handle_error_en(s, "pthread_attr_destroy");
383 s = pthread_join(thread, NULL);
385 handle_error_en(s, "pthread_join");
391 \fBgetrlimit\fP(2), \fBsched_get_priority_min\fP(2), \fBsched_setscheduler\fP(2),
392 \fBpthread_attr_init\fP(3), \fBpthread_attr_setinheritsched\fP(3),
393 \fBpthread_attr_setschedparam\fP(3), \fBpthread_attr_setschedpolicy\fP(3),
394 \fBpthread_create\fP(3), \fBpthread_self\fP(3), \fBpthread_setschedprio\fP(3),
397 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.41 の一部
398 である。プロジェクトの説明とバグ報告に関する情報は
399 http://www.kernel.org/doc/man\-pages/ に書かれている。