OSDN Git Service

(split) LDP: drafts generated from latest pthread/ja.po.
[linuxjm/LDP_man-pages.git] / draft / man3 / pthread_attr_init.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .\"*******************************************************************
25 .\"
26 .\" This file was generated with po4a. Translate the source file.
27 .\"
28 .\"*******************************************************************
29 .TH PTHREAD_ATTR_INIT 3 2008\-11\-11 Linux "Linux Programmer's Manual"
30 .SH 名前
31 pthread_attr_init, pthread_attr_destroy \-
32 スレッド属性オブジェクトの初期化と破棄を行う
33 .SH 書式
34 .nf
35 \fB#include <pthread.h>\fP
36
37 \fBint pthread_attr_init(pthread_attr_t *\fP\fIattr\fP\fB);\fP
38 \fBint pthread_attr_destroy(pthread_attr_t *\fP\fIattr\fP\fB);\fP
39 .sp
40 \fI\-pthread\fP でコンパイルしてリンクする。
41 .fi
42 .SH 説明
43 \fBpthread_attr_init\fP() 関数は \fIattr\fP が指すスレッド属性オブジェクト
44 (thread attributes object) をデフォルトの属性値で初期化する。
45 この呼び出しの後、オブジェクトの各属性は (関連項目に載っている) 種々の
46 関数を使って設定することができ、このオブジェクトはスレッドの作成を行う
47 \fBpthread_create\fP(3) の呼び出しにおいて使用することができる (一つの
48 オブジェクトを複数の \fBpthread_create\fP(3) に渡してもよい)。
49
50 すでに初期化されているスレッド属性オブジェクトに対して
51 \fBpthread_attr_init\fP() を呼び出した場合、
52 どのような動作になるかは不定である。
53
54 スレッド属性オブジェクトがもはや必要なくなった際には、
55 そのオブジェクトは \fBpthread_attr_destroy\fP() 関数を使って破棄すべきである。
56 スレッド属性オブジェクトを破棄しても、
57 そのオブジェクトを使って作成されたスレッドには影響はない。
58
59 いったん破棄したスレッド属性オブジェクトは、
60 \fBpthread_attr_init\fP() を使って再初期化することができる。
61 破棄したスレッド属性オブジェクトをこれ以外の用途で
62 使った場合の結果は不定である。
63 .SH 返り値
64 成功すると、これらの関数は 0 を返す。
65 エラーの場合、0 以外のエラー番号を返す。
66 .SH エラー
67 POSIX.1\-2001 では \fBpthread_attr_init\fP() にはエラー \fBENOMEM\fP が
68 記載されている。
69 Linux では、これらの関数は常に作成する
70 (ただし、移植性や将来も動作することを保証したいアプリケーションでは
71 正のエラーの返り値を処理するようにすべきである)。
72 .SH 準拠
73 POSIX.1\-2001.
74 .SH 注意
75 \fIpthread_attr_t\fP 型の内部構造は意識すべきではない。
76 pthreads 関数経由以外でのオブジェクトへのアクセスは移植性がなく、
77 どのような結果が得られるかも分からない。
78 .SH 例
79 下記のプログラムは、\fBpthread_attr_init\fP() と種々の関連関数を使って、
80 スレッド属性オブジェクトの初期化を行い、
81 そのオブジェクトを使ってスレッドを一つ作成する。
82 作成されたスレッドは、作成後に \fBpthread_getattr_np\fP(3) 関数
83 (非標準の GNU 拡張) を使ってスレッドの属性を取得し、
84 取得した属性を表示する。
85
86 コマンドライン引き数なしでプログラムを実行した場合、
87 \fBpthread_create\fP(3) の \fIattr\fP 引き数には NULL が渡される。
88 この場合、スレッドはデフォルトの属性で作成される。
89 このプログラムを NPTL スレッド実装が使われている Linux/x86\-32 で
90 動作させると、以下のような出力が得られる。
91
92 .in +4n
93 .nf
94 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
95 $\fB ulimit \-s\fP       # No stack imit ==> default stack size is 2MB
96 unlimited
97 $\fB ./a.out\fP
98 Thread attributes:
99         Detach state        = PTHREAD_CREATE_JOINABLE
100         Scope               = PTHREAD_SCOPE_SYSTEM
101         Inherit scheduler   = PTHREAD_INHERIT_SCHED
102         Scheduling policy   = SCHED_OTHER
103         Scheduling priority = 0
104         Guard size          = 4096 bytes
105         Stack address       = 0x40196000
106         Stack size          = 0x201000 bytes
107 .fi
108 .in
109
110 コマンドライン引き数でスタックサイズが与えられた場合、
111 このプログラムは、スレッド属性オブジェクトを初期化し、
112 そのオブジェクトの各種属性を設定し、
113 \fBpthread_create\fP(3) の呼び出しでこのオブジェクトへのポインタを渡す。
114 このプログラムを NPTL スレッド実装が使われている Linux/x86\-32 で
115 動作させると、以下のような出力が得られる。
116
117 .in +4n
118 .nf
119 .\" Results from glibc 2.8, SUSE 11.0; Oct 2008
120 $\fB ./a.out 0x3000000\fP
121 posix_memalign() allocated at 0x40197000
122 Thread attributes:
123         Detach state        = PTHREAD_CREATE_DETACHED
124         Scope               = PTHREAD_SCOPE_SYSTEM
125         Inherit scheduler   = PTHREAD_EXPLICIT_SCHED
126         Scheduling policy   = SCHED_OTHER
127         Scheduling priority = 0
128         Guard size          = 0 bytes
129         Stack address       = 0x40197000
130         Stack size          = 0x3000000 bytes
131 .fi
132 .in
133 .SS プログラムのソース
134 \&
135 .nf
136 #define _GNU_SOURCE     /* To get pthread_getattr_np() declaration */
137 #include <pthread.h>
138 #include <stdio.h>
139 #include <stdlib.h>
140 #include <unistd.h>
141 #include <errno.h>
142
143 #define handle_error_en(en, msg) \e
144         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
145
146 static void
147 display_pthread_attr(pthread_attr_t *attr, char *prefix)
148 {
149     int s, i;
150     size_t v;
151     void *stkaddr;
152     struct sched_param sp;
153
154     s = pthread_attr_getdetachstate(attr, &i);
155     if (s != 0)
156         handle_error_en(s, "pthread_attr_getdetachstate");
157     printf("%sDetach state        = %s\en", prefix,
158             (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" :
159             (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" :
160             "???");
161
162     s = pthread_attr_getscope(attr, &i);
163     if (s != 0)
164         handle_error_en(s, "pthread_attr_getscope");
165     printf("%sScope               = %s\en", prefix,
166             (i == PTHREAD_SCOPE_SYSTEM)  ? "PTHREAD_SCOPE_SYSTEM" :
167             (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" :
168             "???");
169
170     s = pthread_attr_getinheritsched(attr, &i);
171     if (s != 0)
172         handle_error_en(s, "pthread_attr_getinheritsched");
173     printf("%sInherit scheduler   = %s\en", prefix,
174             (i == PTHREAD_INHERIT_SCHED)  ? "PTHREAD_INHERIT_SCHED" :
175             (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" :
176             "???");
177
178     s = pthread_attr_getschedpolicy(attr, &i);
179     if (s != 0)
180         handle_error_en(s, "pthread_attr_getschedpolicy");
181     printf("%sScheduling policy   = %s\en", prefix,
182             (i == SCHED_OTHER) ? "SCHED_OTHER" :
183             (i == SCHED_FIFO)  ? "SCHED_FIFO" :
184             (i == SCHED_RR)    ? "SCHED_RR" :
185             "???");
186
187     s = pthread_attr_getschedparam(attr, &sp);
188     if (s != 0)
189         handle_error_en(s, "pthread_attr_getschedparam");
190     printf("%sScheduling priority = %d\en", prefix, sp.sched_priority);
191
192     s = pthread_attr_getguardsize(attr, &v);
193     if (s != 0)
194         handle_error_en(s, "pthread_attr_getguardsize");
195     printf("%sGuard size          = %d bytes\en", prefix, v);
196
197     s = pthread_attr_getstack(attr, &stkaddr, &v);
198     if (s != 0)
199         handle_error_en(s, "pthread_attr_getstack");
200     printf("%sStack address       = %p\en", prefix, stkaddr);
201     printf("%sStack size          = 0x%x bytes\en", prefix, v);
202 }
203
204 static void *
205 thread_start(void *arg)
206 {
207     int s;
208     pthread_attr_t gattr;
209
210     /* pthread_getattr_np() is a non\-standard GNU extension that
211        retrieves the attributes of the thread specified in its
212        first argument */
213
214     s = pthread_getattr_np(pthread_self(), &gattr);
215     if (s != 0)
216         handle_error_en(s, "pthread_getattr_np");
217
218     printf("Thread attributes:\en");
219     display_pthread_attr(&gattr, "\et");
220
221     exit(EXIT_SUCCESS);         /* Terminate all threads */
222 }
223
224 int
225 main(int argc, char *argv[])
226 {
227     pthread_t thr;
228     pthread_attr_t attr;
229     pthread_attr_t *attrp;      /* NULL or &attr */
230     int s;
231
232     attrp = NULL;
233
234     /* If a command\-line argument was supplied, use it to set the
235        stack\-size attribute and set a few other thread attributes,
236        and set attrp pointing to thread attributes object */
237
238     if (argc > 1) {
239         int stack_size;
240         void *sp;
241
242         attrp = &attr;
243
244         s = pthread_attr_init(&attr);
245         if (s != 0)
246             handle_error_en(s, "pthread_attr_init");
247
248         s = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
249         if (s != 0)
250             handle_error_en(s, "pthread_attr_setdetachstate");
251
252         s = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
253         if (s != 0)
254             handle_error_en(s, "pthread_attr_setinheritsched");
255
256         stack_size = strtoul(argv[1], NULL, 0);
257
258         s = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stack_size);
259         if (s != 0)
260             handle_error_en(s, "posix_memalign");
261
262         printf("posix_memalign() allocated at %p\en", sp);
263
264         s = pthread_attr_setstack(&attr, sp, stack_size);
265         if (s != 0)
266             handle_error_en(s, "pthread_attr_setstack");
267     }
268
269     s = pthread_create(&thr, attrp, &thread_start, NULL);
270     if (s != 0)
271         handle_error_en(s, "pthread_create");
272
273     if (attrp != NULL) {
274         s = pthread_attr_destroy(attrp);
275         if (s != 0)
276             handle_error_en(s, "pthread_attr_destroy");
277     }
278
279     pause();    /* Terminates when other thread calls exit() */
280 }
281 .fi
282 .SH 関連項目
283 \fBpthread_attr_setaffinity_np\fP(3), \fBpthread_attr_setdetachstate\fP(3),
284 \fBpthread_attr_setguardsize\fP(3), \fBpthread_attr_setinheritsched\fP(3),
285 \fBpthread_attr_setschedparam\fP(3), \fBpthread_attr_setschedpolicy\fP(3),
286 \fBpthread_attr_setscope\fP(3), \fBpthread_attr_setstack\fP(3),
287 \fBpthread_attr_setstackaddr\fP(3), \fBpthread_attr_setstacksize\fP(3),
288 \fBpthread_create\fP(3), \fBpthread_getattr_np\fP(3), \fBpthreads\fP(7)
289 .SH この文書について
290 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
291 である。プロジェクトの説明とバグ報告に関する情報は
292 http://www.kernel.org/doc/man\-pages/ に書かれている。