1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\"*******************************************************************
28 .\" This file was generated with po4a. Translate the source file.
30 .\"*******************************************************************
32 .\" Japanese Version Copyright (c) 2012 Akihiro MOTOKI
33 .\" all rights reserved.
34 .\" Translated 2012-05-04, Akihiro MOTOKI <amotoki@gmail.com>
36 .TH PTHREAD_SETSCHEDPARAM 3 2008\-11\-17 Linux "Linux Programmer's Manual"
38 pthread_setschedparam, pthread_getschedparam \- スレッドの
39 スケジューリングポリシーとパラメータを設定/取得する
42 \fB#include <pthread.h>\fP
44 \fBpthread_setschedparam(pthread_t \fP\fIthread\fP\fB, int \fP\fIpolicy\fP\fB,\fP
45 \fB const struct sched_param *\fP\fIparam\fP\fB);\fP
46 \fBpthread_getschedparam(pthread_t \fP\fIthread\fP\fB, int *\fP\fIpolicy\fP\fB,\fP
47 \fB struct sched_param *\fP\fIparam\fP\fB);\fP
49 \fI\-pthread\fP でコンパイルしてリンクする。
52 \fBpthread_setschedparam\fP() 関数は、スレッド \fIthread\fP の
53 スケジューリングポリシーとスケジューリングパラメータを設定する。
55 .\" FIXME . pthread_setschedparam() places no restriction on the policy,
56 .\" but pthread_attr_setschedpolicy() restricts policy to RR/FIFO/OTHER
57 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7013
58 \fIpolicy\fP は \fIthread\fP の新しいスケジューリングポリシーを指定する。
59 \fIpolicy\fP に指定できる値とその意味は \fBsched_setscheduler\fP(2) で
62 \fIparam\fP が指す構造体は \fIthread\fP の新しいスケジューリングパラメータを
63 指定する。スケジューリングパラメータは以下の構造体で管理される。
68 int sched_priority; /* Scheduling priority */
73 見て分かる通り、サポートされているスケジューリングパラメータは一つだけ
74 である。各スケジューリングポリシーで許可されるスケジューリング優先度の
75 詳細については、\fBsched_setscheduler\fP(2) を参照のこと。
77 .\" FIXME . nptl/pthread_setschedparam.c has the following
78 .\" /* If the thread should have higher priority because of some
79 .\" PTHREAD_PRIO_PROTECT mutexes it holds, adjust the priority. */
80 .\" Eventually (perhaps after writing the mutexattr pages), we
81 .\" may want to add something on the topic to this page.
82 \fBpthread_getschedparam\fP() 関数は、スレッド \fIthread\fP の
84 それぞれ \fIpolicy\fP と \fIparam\fP が指すバッファに入れて返す。
85 返された優先度の値は、最も最近実行した \fIthread\fP に影響を与える
86 \fBpthread_setschedparam\fP(), \fBpthread_setschedprio\fP,
87 \fBpthread_create\fP で設定された値となる。
88 返された優先度は、優先度の継承や優先度の上限を設定する関数
89 (例えば \fBpthread_mutexattr_setprioceiling\fP(3) や
90 \fBpthread_mutexattr_setprotocol\fP(3) を参照) の呼び出しの結果
91 行われる一時的な優先度の調整の影響を受けない。
95 \fBpthread_setschedparam\fP() が失敗した場合、 \fIthread\fP の
96 スケジューリングポリシーとパラメータは変更されない。
98 これらの関数はどちらも以下のエラーで失敗する場合がある。
101 ID が \fIthread\fP のスレッドが見つからなかった。
103 \fBpthread_setschedparam\fP() はさらに以下のエラーで失敗する場合がある。
106 \fIpolicy\fP が認識できないポリシーであるか、
107 \fIparam\fP が \fIpolicy\fP では意味を持たない値である。
110 呼び出し側が、指定されたスケジューリングポリシーやパラメータを設定する
114 .\" Available since glibc 2.0
115 POSIX.1\-2001 では、 \fBpthread_setschedparam\fP() に関して
116 エラー \fBENOTSUP\fP ("サポートされていない値をスケジューリングポリシーや
117 パラメータに設定しようとした") も追加で規定されている。
121 スレッドのスケジューリングポリシーや優先度を変更するために必要な許可や
122 変更した場合の影響、および各スケジューリングポリシーで認められる優先度
123 の範囲の詳細については、 \fBsched_setscheduler\fP(2) を参照。
125 以下のプログラムは \fBpthread_setschedparam\fP() と
126 \fBpthread_getschedparam\fP() やスケジューリングに関連する pthreads の
127 他のいろいろな関数の使用例を示すものである。
129 以下の実行例では、メインスレッドは、自分のスケジューリングポリシーを
130 優先度 10 の \fBSCHED_FIFO\fP を設定し、スレッド属性オブジェクトを
131 スケジューリングポリシー属性 \fBSCHED_RR\fP とスケジューリング優先度
133 次に、このプログラムは (\fBpthread_attr_setinheritsched\fP(3) を使って)
134 そのスレッド属性オブジェクトの inherit scheduler 属性に
135 \fBPTHREAD_EXPLICIT_SCHED\fP を設定する。\fBPTHREAD_EXPLICIT_SCHED\fP は、
136 そのスレッド属性オブジェクトを使って作成されたスレッドはスレッド属性
137 オブジェクトからスケジューリング属性を取得して使うことを意味する。
138 それから、このスレッド属性オブジェクトを使ってスレッドを作成し、
139 作成したスレッドのスケジューリングポリシーと優先度を表示する。
143 $ \fBsu\fP # Need privilege to set real\-time scheduling policies
145 # \fB./a.out \-mf10 \-ar20 \-i e\fP
146 Scheduler settings of main thread
147 policy=SCHED_FIFO, priority=10
149 Scheduler settings in \(aqattr\(aq
150 policy=SCHED_RR, priority=20
151 inheritsched is EXPLICIT
153 Scheduler attributes of new thread
154 policy=SCHED_RR, priority=20
158 上記の出力では、スケジューリングポリシーと優先度がスレッド属性
159 オブジェクトで指定された値から取られていることが分かる。
161 次の実行例は前のものと同じだが、 inherit scheduler 属性が
162 \fBPTHREAD_INHERIT_SCHED\fP に設定される点が異なる。
163 \fBPTHREAD_INHERIT_SCHED\fP は、そのスレッド属性オブジェクトを使って作成
164 されたスレッドは、スレッド属性オブジェクトからスケジューリング属性を
165 無視し、代わりに呼び出したスレッドからスケジューリング属性を取得する
170 # \fB./a.out \-mf10 \-ar20 \-i i\fP
171 Scheduler settings of main thread
172 policy=SCHED_FIFO, priority=10
174 Scheduler settings in \(aqattr\(aq
175 policy=SCHED_RR, priority=20
176 inheritsched is INHERIT
178 Scheduler attributes of new thread
179 policy=SCHED_FIFO, priority=10
183 上記の出力では、スケジューリングポリシーと優先度が、
185 スレッドを作成したスレッドから取れれていることが分かる。
187 なお、 \fI\-i\ i\fP を省略した場合でも、
188 \fBPTHREAD_INHERIT_SCHED\fP が inherit scheduler 属性のデフォルト値なので、
193 /* pthreads_sched_test.c */
201 #define handle_error_en(en, msg) \e
202 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
205 usage(char *prog_name, char *msg)
210 fprintf(stderr, "Usage: %s [options]\en", prog_name);
211 fprintf(stderr, "Options are:\en");
212 #define fpe(msg) fprintf(stderr, "\et%s", msg); /* Shorter */
213 fpe("\-a<policy><prio> Set scheduling policy and priority in\en");
214 fpe(" thread attributes object\en");
215 fpe(" <policy> can be\en");
216 fpe(" f SCHED_FIFO\en");
217 fpe(" r SCHED_RR\en");
218 fpe(" o SCHED_OTHER\en");
219 fpe("\-A Use default thread attributes object\en");
220 fpe("\-i {e|s} Set inherit scheduler attribute to\en");
221 fpe(" \(aqexplicit\(aq or \(aqinherit\(aq\en");
222 fpe("\-m<policy><prio> Set scheduling policy and priority on\en");
223 fpe(" main thread before pthread_create() call\en");
228 get_policy(char p, int *policy)
231 case \(aqf\(aq: *policy = SCHED_FIFO; return 1;
232 case \(aqr\(aq: *policy = SCHED_RR; return 1;
233 case \(aqo\(aq: *policy = SCHED_OTHER; return 1;
239 display_sched_attr(int policy, struct sched_param *param)
241 printf(" policy=%s, priority=%d\en",
242 (policy == SCHED_FIFO) ? "SCHED_FIFO" :
243 (policy == SCHED_RR) ? "SCHED_RR" :
244 (policy == SCHED_OTHER) ? "SCHED_OTHER" :
246 param\->sched_priority);
250 display_thread_sched_attr(char *msg)
253 struct sched_param param;
255 s = pthread_getschedparam(pthread_self(), &policy, ¶m);
257 handle_error_en(s, "pthread_getschedparam");
259 printf("%s\en", msg);
260 display_sched_attr(policy, ¶m);
264 thread_start(void *arg)
266 display_thread_sched_attr("Scheduler attributes of new thread");
272 main(int argc, char *argv[])
274 int s, opt, inheritsched, use_null_attrib, policy;
277 pthread_attr_t *attrp;
278 char *attr_sched_str, *main_sched_str, *inheritsched_str;
279 struct sched_param param;
281 /* Process command\-line options */
284 attr_sched_str = NULL;
285 main_sched_str = NULL;
286 inheritsched_str = NULL;
288 while ((opt = getopt(argc, argv, "a:Ai:m:")) != \-1) {
290 case \(aqa\(aq: attr_sched_str = optarg; break;
291 case \(aqA\(aq: use_null_attrib = 1; break;
292 case \(aqi\(aq: inheritsched_str = optarg; break;
293 case \(aqm\(aq: main_sched_str = optarg; break;
294 default: usage(argv[0], "Unrecognized option\en");
298 if (use_null_attrib &&
299 (inheritsched_str != NULL || attr_sched_str != NULL))
300 usage(argv[0], "Can\(aqt specify \-A with \-i or \-a\en");
302 /* Optionally set scheduling attributes of main thread,
303 and display the attributes */
305 if (main_sched_str != NULL) {
306 if (!get_policy(main_sched_str[0], &policy))
307 usage(argv[0], "Bad policy for main thread (\-s)\en");
308 param.sched_priority = strtol(&main_sched_str[1], NULL, 0);
310 s = pthread_setschedparam(pthread_self(), policy, ¶m);
312 handle_error_en(s, "pthread_setschedparam");
315 display_thread_sched_attr("Scheduler settings of main thread");
318 /* Initialize thread attributes object according to options */
322 if (!use_null_attrib) {
323 s = pthread_attr_init(&attr);
325 handle_error_en(s, "pthread_attr_init");
329 if (inheritsched_str != NULL) {
330 if (inheritsched_str[0] == \(aqe\(aq)
331 inheritsched = PTHREAD_EXPLICIT_SCHED;
332 else if (inheritsched_str[0] == \(aqi\(aq)
333 inheritsched = PTHREAD_INHERIT_SCHED;
335 usage(argv[0], "Value for \-i must be \(aqe\(aq or \(aqi\(aq\en");
337 s = pthread_attr_setinheritsched(&attr, inheritsched);
339 handle_error_en(s, "pthread_attr_setinheritsched");
342 if (attr_sched_str != NULL) {
343 if (!get_policy(attr_sched_str[0], &policy))
345 "Bad policy for \(aqattr\(aq (\-a)\en");
346 param.sched_priority = strtol(&attr_sched_str[1], NULL, 0);
348 s = pthread_attr_setschedpolicy(&attr, policy);
350 handle_error_en(s, "pthread_attr_setschedpolicy");
351 s = pthread_attr_setschedparam(&attr, ¶m);
353 handle_error_en(s, "pthread_attr_setschedparam");
356 /* If we initialized a thread attributes object, display
357 the scheduling attributes that were set in the object */
360 s = pthread_attr_getschedparam(&attr, ¶m);
362 handle_error_en(s, "pthread_attr_getschedparam");
363 s = pthread_attr_getschedpolicy(&attr, &policy);
365 handle_error_en(s, "pthread_attr_getschedpolicy");
367 printf("Scheduler settings in \(aqattr\(aq\en");
368 display_sched_attr(policy, ¶m);
370 s = pthread_attr_getinheritsched(&attr, &inheritsched);
371 printf(" inheritsched is %s\en",
372 (inheritsched == PTHREAD_INHERIT_SCHED) ? "INHERIT" :
373 (inheritsched == PTHREAD_EXPLICIT_SCHED) ? "EXPLICIT" :
378 /* Create a thread that will display its scheduling attributes */
380 s = pthread_create(&thread, attrp, &thread_start, NULL);
382 handle_error_en(s, "pthread_create");
384 /* Destroy unneeded thread attributes object */
386 s = pthread_attr_destroy(&attr);
388 handle_error_en(s, "pthread_attr_destroy");
390 s = pthread_join(thread, NULL);
392 handle_error_en(s, "pthread_join");
400 \fBgetrlimit\fP(2), \fBsched_get_priority_min\fP(2), \fBsched_setscheduler\fP(2),
401 \fBpthread_attr_init\fP(3), \fBpthread_attr_setinheritsched\fP(3),
402 \fBpthread_attr_setschedparam\fP(3), \fBpthread_attr_setschedpolicy\fP(3),
403 \fBpthread_create\fP(3), \fBpthread_self\fP(3), \fBpthread_setschedprio\fP(3),
406 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部
407 である。プロジェクトの説明とバグ報告に関する情報は
408 http://www.kernel.org/doc/man\-pages/ に書かれている。