OSDN Git Service

Update release for LDP 3.67
[linuxjm/LDP_man-pages.git] / release / man3 / pthread_setname_np.3
1 .\" Copyright (C) 2012 Chandan Apsangi <chandan.jc@gmail.com>
2 .\" and Copyright (C) 2013 Michael Kerrisk <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 .\"*******************************************************************
27 .\"
28 .\" This file was generated with po4a. Translate the source file.
29 .\"
30 .\"*******************************************************************
31 .\"
32 .\" Japanese Version Copyright (c) 2013  Akihiro MOTOKI
33 .\"         all rights reserved.
34 .\" Translated 2013-05-06, Akihiro MOTOKI <amotoki@gmail.com>
35 .\" Updated 2013-07-17, Akihiro MOTOKI <amotoki@gmail.com>
36 .\"
37 .TH PTHREAD_SETNAME_NP 3 2013\-06\-21 Linux "Linux Programmer's Manual"
38 .SH 名前
39 pthread_setname_np, pthread_getname_np \- スレッド名の設定/取得を行う
40 .SH 書式
41 .nf
42 \fB#define _GNU_SOURCE\fP             /* feature_test_macros(7) 参照 */
43 \fB#include <pthread.h>\fP
44 \fBint pthread_setname_np(pthread_t \fP\fIthread\fP\fB, const char *\fP\fIname\fP\fB);\fP
45 \fBint pthread_getname_np(pthread_t \fP\fIthread\fP\fB,\fP
46 \fB                       const char *\fP\fIname\fP\fB, size_t \fP\fIlen\fP\fB);\fP
47 .fi
48 .sp
49 \fI\-pthread\fP を付けてコンパイルとリンクを行う。
50 .SH 説明
51 デフォルトでは、 \fBpthread_create\fP() で作成されたすべてのスレッドはプログラム名を継承する。
52 \fBpthread_setname_np\fP() 関数を使うとスレッドに固有の名前を設定することができる。
53 スレッド固有の名前はマルチスレッド・アプリケーションのデバッグに便利である。 スレッド名は意味のある C 言語の文字列である。 その長さは 16
54 文字に限定されており、 終端のヌルバイト (\(aq\e0\(aq) も 16 文字に含まれる。 \fIthread\fP
55 引き数で名前を変更するスレッドを指定する。 \fIname\fP には新しい名前を指定する。
56
57 \fBpthread_getname_np\fP() 関数を使うと、 スレッド名を取得することができる。 \fIthread\fP
58 引き数は名前を取得するスレッドを指定する。 バッファ \fIname\fP はスレッド名を返すのに使用される。 \fIlen\fP には \fIname\fP
59 の大きさをバイトで指定する。 \fIname\fP で指定されたバッファの大きさは最低でも 16 文字とすべきである。
60 出力バッファに返されたスレッド名はヌル終端される。
61 .SH 返り値
62 成功すると、これらの関数は 0 を返す。
63 エラーの場合、0 以外のエラー番号を返す。
64 .SH エラー
65 \fBpthread_setname_np\fP() は以下のエラーで失敗する場合がある。
66 .TP 
67 \fBERANGE\fP
68 \fIname\fP で指定された文字列の長さが、許可されている上限を超えている。
69 .PP
70 \fBpthread_getname_np\fP() は以下のエラーで失敗する場合がある。
71 .TP 
72 \fBERANGE\fP
73 \fIname\fP と \fIlen\fP で指定されたバッファが、 スレッド名を格納するには短かすぎる。
74 .PP
75 \fI/proc/self/task/[tid]/comm\fP のオープンに失敗した場合、 これらの関数は \fBopen\fP(2)
76 で説明されているエラーのいずれかで失敗する。
77 .SH バージョン
78 これらの関数は glibc バージョン 2.12 で初めて登場した。
79 .SH 準拠
80 これらの関数は非標準の GNU による拡張である。
81 .SH 注意
82 \fBpthread_setname_np\fP() は内部で \fI/proc\fP ファイルシステムのスレッド固有の \fIcomm\fP ファイル
83 (\fI/proc/self/task/[tid]/comm\fP) に書き込みを行う。 \fBpthread_getname_np\fP()
84 はこのファイルから読み出しを行う。
85 .SH 例
86 .PP
87 以下のプログラムは、 \fBpthread_setname_np\fP() と \fBpthread_getname_np\fP()
88 の使用例を示している。
89
90 以下のシェルセッションは、このプログラムの実行例である。
91 .in +4n
92 .nf
93
94 $\fB ./a.out\fP
95 Created a thread. Default name is: a.out
96 The thread name after setting it is THREADFOO.
97 \fB^Z\fP                           # Suspend the program
98 [1]+  Stopped           ./a.out
99 $ \fBps H \-C a.out \-o 'pid tid cmd comm'\fP
100   PID   TID CMD                         COMMAND
101  5990  5990 ./a.out                     a.out
102  5990  5991 ./a.out                     THREADFOO
103 $ \fBcat /proc/5990/task/5990/comm\fP
104 a.out
105 $ \fBcat /proc/5990/task/5991/comm\fP
106 THREADFOO
107 .fi
108 .in
109 .SS プログラムのソース
110 \&
111 .nf
112 #define _GNU_SOURCE
113 #include <pthread.h>
114 #include <stdio.h>
115 #include <string.h>
116 #include <unistd.h>
117 #include <errno.h>
118 #include <stdlib.h>
119
120 #define NAMELEN 16
121
122 #define errExitEN(en, msg) \e
123             do { errno = en; perror(msg); exit(EXIT_FAILURE); \e
124         } while (0)
125
126 static void *
127 threadfunc(void *parm)
128 {
129     sleep(5);          // allow main program to set the thread name
130     return NULL;
131 }
132
133 int
134 main(int argc, char **argv)
135 {
136     pthread_t thread;
137     int rc;
138     char thread_name[NAMELEN];
139
140     rc = pthread_create(&thread, NULL, threadfunc, NULL);
141     if (rc != 0)
142         errExitEN(rc, "pthread_create");
143
144     rc = pthread_getname_np(thread, thread_name, NAMELEN);
145     if (rc != 0)
146         errExitEN(rc, "pthread_getname_np");
147
148     printf("Created a thread. Default name is: %s\en", thread_name);
149     rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
150     if (rc != 0)
151         errExitEN(rc, "pthread_setname_np");
152
153     sleep(2);
154
155     rc = pthread_getname_np(thread, thread_name,
156                             (argc > 2) ? atoi(argv[1]) : NAMELEN);
157     if (rc != 0)
158         errExitEN(rc, "pthread_getname_np");
159     printf("The thread name after setting it is %s.\en", thread_name);
160
161     rc = pthread_join(thread, NULL);
162     if (rc != 0)
163         errExitEN(rc, "pthread_join");
164
165     printf("Done\en");
166     exit(EXIT_SUCCESS);
167 }
168 .fi
169 .SH 関連項目
170 .ad l
171 .nh
172 \fBprctl\fP(2), \fBpthread_create\fP(3), \fBpthreads\fP(7)
173 .SH この文書について
174 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.67 の一部
175 である。プロジェクトの説明とバグ報告に関する情報は
176 http://www.kernel.org/doc/man\-pages/ に書かれている。