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