OSDN Git Service

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