.\" Copyright (C) 2012 Chandan Apsangi .\" and Copyright (C) 2013 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2013 Akihiro MOTOKI .\" all rights reserved. .\" Translated 2013-05-06, Akihiro MOTOKI .\" Updated 2013-07-17, Akihiro MOTOKI .\" .TH PTHREAD_SETNAME_NP 3 2014\-05\-28 Linux "Linux Programmer's Manual" .SH 名前 pthread_setname_np, pthread_getname_np \- スレッド名の設定/取得を行う .SH 書式 .nf \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP \fBint pthread_setname_np(pthread_t \fP\fIthread\fP\fB, const char *\fP\fIname\fP\fB);\fP \fBint pthread_getname_np(pthread_t \fP\fIthread\fP\fB,\fP \fB char *\fP\fIname\fP\fB, size_t \fP\fIlen\fP\fB);\fP .fi .sp \fI\-pthread\fP を付けてコンパイルとリンクを行う。 .SH 説明 デフォルトでは、 \fBpthread_create\fP() で作成されたすべてのスレッドはプログラム名を継承する。 \fBpthread_setname_np\fP() 関数を使うとスレッドに固有の名前を設定することができる。 スレッド固有の名前はマルチスレッドアプリケーションのデバッグに便利である。 スレッド名は意味のある C 言語の文字列である。 その長さは 16 文字に限定されており、 終端のヌルバイト (\(aq\e0\(aq) も 16 文字に含まれる。 \fIthread\fP 引き数で名前を変更するスレッドを指定する。 \fIname\fP には新しい名前を指定する。 \fBpthread_getname_np\fP() 関数を使うと、 スレッド名を取得することができる。 \fIthread\fP 引き数は名前を取得するスレッドを指定する。 バッファー \fIname\fP はスレッド名を返すのに使用される。 \fIlen\fP には \fIname\fP の大きさをバイトで指定する。 \fIname\fP で指定されたバッファーの大きさは最低でも 16 文字とすべきである。 出力バッファーに返されたスレッド名はヌル終端される。 .SH 返り値 成功すると、これらの関数は 0 を返す。 エラーの場合、0 以外のエラー番号を返す。 .SH エラー \fBpthread_setname_np\fP() は以下のエラーで失敗する場合がある。 .TP \fBERANGE\fP \fIname\fP で指定された文字列の長さが、許可されている上限を超えている。 .PP \fBpthread_getname_np\fP() は以下のエラーで失敗する場合がある。 .TP \fBERANGE\fP \fIname\fP と \fIlen\fP で指定されたバッファーが、 スレッド名を格納するには短かすぎる。 .PP \fI/proc/self/task/[tid]/comm\fP のオープンに失敗した場合、 これらの関数は \fBopen\fP(2) で説明されているエラーのいずれかで失敗する。 .SH バージョン これらの関数は glibc バージョン 2.12 で初めて登場した。 .SH 準拠 これらの関数は非標準の GNU による拡張である。 .SH 注意 \fBpthread_setname_np\fP() は内部で \fI/proc\fP ファイルシステムのスレッド固有の \fIcomm\fP ファイル (\fI/proc/self/task/[tid]/comm\fP) に書き込みを行う。 \fBpthread_getname_np\fP() はこのファイルから読み出しを行う。 .SH 例 .PP 以下のプログラムは、 \fBpthread_setname_np\fP() と \fBpthread_getname_np\fP() の使用例を示している。 以下のシェルセッションは、このプログラムの実行例である。 .in +4n .nf $\fB ./a.out\fP Created a thread. Default name is: a.out The thread name after setting it is THREADFOO. \fB^Z\fP # Suspend the program [1]+ Stopped ./a.out $ \fBps H \-C a.out \-o 'pid tid cmd comm'\fP PID TID CMD COMMAND 5990 5990 ./a.out a.out 5990 5991 ./a.out THREADFOO $ \fBcat /proc/5990/task/5990/comm\fP a.out $ \fBcat /proc/5990/task/5991/comm\fP THREADFOO .fi .in .SS プログラムのソース \& .nf #define _GNU_SOURCE #include #include #include #include #include #include #define NAMELEN 16 #define errExitEN(en, msg) \e do { errno = en; perror(msg); exit(EXIT_FAILURE); \e } while (0) static void * threadfunc(void *parm) { sleep(5); // allow main program to set the thread name return NULL; } int main(int argc, char **argv) { pthread_t thread; int rc; char thread_name[NAMELEN]; rc = pthread_create(&thread, NULL, threadfunc, NULL); if (rc != 0) errExitEN(rc, "pthread_create"); rc = pthread_getname_np(thread, thread_name, NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("Created a thread. Default name is: %s\en", thread_name); rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO"); if (rc != 0) errExitEN(rc, "pthread_setname_np"); sleep(2); rc = pthread_getname_np(thread, thread_name, (argc > 2) ? atoi(argv[1]) : NAMELEN); if (rc != 0) errExitEN(rc, "pthread_getname_np"); printf("The thread name after setting it is %s.\en", thread_name); rc = pthread_join(thread, NULL); if (rc != 0) errExitEN(rc, "pthread_join"); printf("Done\en"); exit(EXIT_SUCCESS); } .fi .SH 関連項目 .ad l .nh \fBprctl\fP(2), \fBpthread_create\fP(3), \fBpthreads\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。