OSDN Git Service

6ed182cff576ecdaafb96893d9c8cdbbc1c1e47a
[linuxjm/LDP_man-pages.git] / original / 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 .TH PTHREAD_SETNAME_NP 3 2013-06-21 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_setname_np, pthread_getname_np \- set/get the name of a thread
29 .SH SYNOPSIS
30 .nf
31 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
32 .B #include <pthread.h>
33 .BI "int pthread_setname_np(pthread_t " thread ", const char *" name ");
34 .BI "int pthread_getname_np(pthread_t " thread ,
35 .BI "                       const char *" name ", size_t " len );
36 .fi
37 .sp
38 Compile and link with \fI\-pthread\fP.
39 .SH DESCRIPTION
40 By default, all the threads created using
41 .BR pthread_create ()
42 inherit the program name.
43 The
44 .BR pthread_setname_np ()
45 function can be used to set a unique name for a thread,
46 which can be useful for debugging
47 multithreaded applications.
48 The thread name is a meaningful C language string, whose length is
49 restricted to 16 characters, including the terminating null byte (\(aq\\0\(aq).
50 The
51 .I thread
52 argument specifies the thread whose name is to be changed;
53 .I name
54 specifies the new name.
55
56 The
57 .BR pthread_getname_np ()
58 function can be used to retrieve the name of the thread.
59 The
60 .I thread
61 argument specifies the thread whose name is to be retrieved.
62 The buffer
63 .I name
64 is used to return the thread name;
65 .I len
66 specifies the number of bytes available in
67 .IR name .
68 The buffer specified by
69 .I name
70 should be at least 16 characters in length.
71 The returned thread name in the output buffer will be null terminated.
72 .SH RETURN VALUE
73 On success, these functions return 0;
74 on error, they return a nonzero error number.
75 .SH ERRORS
76 The
77 .BR pthread_setname_np ()
78 function can fail with the following error:
79 .TP
80 .B ERANGE
81 The length of the string specified pointed to by
82 .I name
83 exceeds the allowed limit.
84 .PP
85 The
86 .BR pthread_getname_np ()
87 function can fail with the following error:
88 .TP
89 .B ERANGE
90 The buffer specified by
91 .I name
92 and
93 .I len
94 is too small to hold the thread name.
95 .PP
96 If either of these functions fails to open
97 .IR /proc/self/task/[tid]/comm ,
98 then the call may fail with one of the errors described in
99 .BR open (2).
100 .SH VERSIONS
101 These functions first appeared in glibc in version 2.12.
102 .SH CONFORMING TO
103 These functions are nonstandard GNU extensions.
104 .SH NOTES
105 .BR pthread_setname_np ()
106 internally writes to the thread-specific
107 .I comm
108 file under the
109 .IR /proc
110 filesystem:
111 .IR /proc/self/task/[tid]/comm .
112 .BR pthread_getname_np ()
113 retrieves it from the same location.
114 .SH EXAMPLE
115 .PP
116 The program below demonstrates the use of
117 .BR pthread_setname_np ()
118 and
119 .BR pthread_getname_np ().
120
121 The following shell session shows a sample run of the program:
122 .in +4n
123 .nf
124
125 .RB "$" " ./a.out"
126 Created a thread. Default name is: a.out
127 The thread name after setting it is THREADFOO.
128 \fB^Z\fP                           # Suspend the program
129 [1]+  Stopped           ./a.out
130 .RB "$ " "ps H -C a.out -o 'pid tid cmd comm'"
131   PID   TID CMD                         COMMAND
132  5990  5990 ./a.out                     a.out
133  5990  5991 ./a.out                     THREADFOO
134 .RB "$ " "cat /proc/5990/task/5990/comm"
135 a.out
136 .RB "$ " "cat /proc/5990/task/5991/comm"
137 THREADFOO
138 .fi
139 .in
140 .SS Program source
141 \&
142 .nf
143 #define _GNU_SOURCE
144 #include <pthread.h>
145 #include <stdio.h>
146 #include <string.h>
147 #include <unistd.h>
148 #include <errno.h>
149 #include <stdlib.h>
150
151 #define NAMELEN 16
152
153 #define errExitEN(en, msg) \\
154             do { errno = en; perror(msg); exit(EXIT_FAILURE); \\
155         } while (0)
156
157 static void *
158 threadfunc(void *parm)
159 {
160     sleep(5);          // allow main program to set the thread name
161     return NULL;
162 }
163
164 int
165 main(int argc, char **argv)
166 {
167     pthread_t thread;
168     int rc;
169     char thread_name[NAMELEN];
170
171     rc = pthread_create(&thread, NULL, threadfunc, NULL);
172     if (rc != 0)
173         errExitEN(rc, "pthread_create");
174
175     rc = pthread_getname_np(thread, thread_name, NAMELEN);
176     if (rc != 0)
177         errExitEN(rc, "pthread_getname_np");
178
179     printf("Created a thread. Default name is: %s\\n", thread_name);
180     rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
181     if (rc != 0)
182         errExitEN(rc, "pthread_setname_np");
183
184     sleep(2);
185
186     rc = pthread_getname_np(thread, thread_name,
187                             (argc > 2) ? atoi(argv[1]) : NAMELEN);
188     if (rc != 0)
189         errExitEN(rc, "pthread_getname_np");
190     printf("The thread name after setting it is %s.\\n", thread_name);
191
192     rc = pthread_join(thread, NULL);
193     if (rc != 0)
194         errExitEN(rc, "pthread_join");
195
196     printf("Done\\n");
197     exit(EXIT_SUCCESS);
198 }
199 .fi
200 .SH SEE ALSO
201 .ad l
202 .nh
203 .BR prctl (2),
204 .BR pthread_create (3),
205 .BR pthreads (7)
206 .SH COLOPHON
207 This page is part of release 3.67 of the Linux
208 .I man-pages
209 project.
210 A description of the project,
211 information about reporting bugs,
212 and the latest version of this page,
213 can be found at
214 \%http://www.kernel.org/doc/man\-pages/.