]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/pthread_setname_np.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / pthread_setname_np.3
CommitLineData
a1eaacb1 1'\" t
5df98ea9 2.\" Copyright (C) 2012 Chandan Apsangi <chandan.jc@gmail.com>
7d928e0d 3.\" and Copyright (C) 2013 Michael Kerrisk <mtk.manpages@gmail.com>
5df98ea9 4.\"
5fbde956 5.\" SPDX-License-Identifier: Linux-man-pages-copyleft
5df98ea9 6.\"
4c1c5274 7.TH pthread_setname_np 3 (date) "Linux man-pages (unreleased)"
5df98ea9
MK
8.SH NAME
9pthread_setname_np, pthread_getname_np \- set/get the name of a thread
1cefd9b1
AC
10.SH LIBRARY
11POSIX threads library
8fc3b2cf 12.RI ( libpthread ", " \-lpthread )
5df98ea9
MK
13.SH SYNOPSIS
14.nf
15.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16.B #include <pthread.h>
c6d039a3 17.P
9bfc9cb1 18.BI "int pthread_setname_np(pthread_t " thread ", const char *" name );
1eed67e7
AC
19.BI "int pthread_getname_np(pthread_t " thread ", char " name [. size "], \
20size_t " size );
5df98ea9 21.fi
5df98ea9
MK
22.SH DESCRIPTION
23By default, all the threads created using
e4a83b01 24.BR pthread_create ()
5df98ea9 25inherit the program name.
e4a83b01
MK
26The
27.BR pthread_setname_np ()
f1a5fff8 28function can be used to set a unique name for a thread,
e4a83b01 29which can be useful for debugging
ce0234ff 30multithreaded applications.
d6c1998e
AC
31The thread name is a meaningful C language string,
32whose length is restricted to 16 characters,
33including the terminating null byte (\[aq]\e0\[aq]).
6350e974
MK
34The
35.I thread
36argument specifies the thread whose name is to be changed;
37.I name
38specifies the new name.
c6d039a3 39.P
5df98ea9 40The
e4a83b01
MK
41.BR pthread_getname_np ()
42function can be used to retrieve the name of the thread.
6350e974
MK
43The
44.I thread
3fd9346e 45argument specifies the thread whose name is to be retrieved.
6350e974
MK
46The buffer
47.I name
48is used to return the thread name;
8c54e23e 49.I size
6350e974
MK
50specifies the number of bytes available in
51.IR name .
e4a83b01
MK
52The buffer specified by
53.I name
6350e974 54should be at least 16 characters in length.
5df98ea9
MK
55The returned thread name in the output buffer will be null terminated.
56.SH RETURN VALUE
57On success, these functions return 0;
58on error, they return a nonzero error number.
59.SH ERRORS
eb6c2cc6
MK
60The
61.BR pthread_setname_np ()
4a2403ed 62function can fail with the following error:
5df98ea9
MK
63.TP
64.B ERANGE
e4a83b01
MK
65The length of the string specified pointed to by
66.I name
67exceeds the allowed limit.
c6d039a3 68.P
4a2403ed
MK
69The
70.BR pthread_getname_np ()
71function can fail with the following error:
72.TP
73.B ERANGE
74The buffer specified by
75.I name
76and
8c54e23e 77.I size
4a2403ed 78is too small to hold the thread name.
c6d039a3 79.P
e4a83b01 80If either of these functions fails to open
0d782b8d 81.IR /proc/self/task/ tid /comm ,
e4a83b01
MK
82then the call may fail with one of the errors described in
83.BR open (2).
412793f9
ZL
84.SH ATTRIBUTES
85For an explanation of the terms used in this section, see
86.BR attributes (7).
87.TS
88allbox;
c466875e 89lbx lb lb
412793f9
ZL
90l l l.
91Interface Attribute Value
92T{
9e54434e
BR
93.na
94.nh
412793f9
ZL
95.BR pthread_setname_np (),
96.BR pthread_getname_np ()
97T} Thread safety MT-Safe
98.TE
3113c7f3 99.SH STANDARDS
4131356c 100GNU;
3ceb0d18 101hence the suffix "_np" (nonportable) in the names.
4131356c
AC
102.SH HISTORY
103glibc 2.12.
5df98ea9 104.SH NOTES
e4a83b01 105.BR pthread_setname_np ()
f2cc5c88 106internally writes to the thread-specific
7d4d1f8a 107.I comm
f2cc5c88 108file under the
1ae6b2c7 109.I /proc
5df98ea9 110filesystem:
0d782b8d 111.IR /proc/self/task/ tid /comm .
e4a83b01 112.BR pthread_getname_np ()
3fd9346e 113retrieves it from the same location.
a14af333 114.SH EXAMPLES
5df98ea9 115The program below demonstrates the use of
e4a83b01
MK
116.BR pthread_setname_np ()
117and
5df98ea9 118.BR pthread_getname_np ().
c6d039a3 119.P
5df98ea9 120The following shell session shows a sample run of the program:
c6d039a3 121.P
5df98ea9 122.in +4n
e646a1ba 123.EX
e4a83b01 124.RB "$" " ./a.out"
5df98ea9
MK
125Created a thread. Default name is: a.out
126The thread name after setting it is THREADFOO.
a1e9245d 127\fB\[ha]Z\fP # Suspend the program
6cfa298b 128[1]+ Stopped ./a.out
b957f81f 129.RB "$ " "ps H \-C a.out \-o \[aq]pid tid cmd comm\[aq]"
6cfa298b
MK
130 PID TID CMD COMMAND
131 5990 5990 ./a.out a.out
132 5990 5991 ./a.out THREADFOO
133.RB "$ " "cat /proc/5990/task/5990/comm"
134a.out
135.RB "$ " "cat /proc/5990/task/5991/comm"
136THREADFOO
b8302363 137.EE
5df98ea9 138.in
5df98ea9
MK
139.SS Program source
140\&
b0b6ab4e 141.\" SRC BEGIN (pthread_setname_np.c)
e7d0bb47 142.EX
5df98ea9 143#define _GNU_SOURCE
5a5208c1 144#include <err.h>
ad3868f0 145#include <errno.h>
5df98ea9
MK
146#include <pthread.h>
147#include <stdio.h>
ad3868f0 148#include <stdlib.h>
5df98ea9
MK
149#include <string.h>
150#include <unistd.h>
fe5dba13 151\&
5df98ea9 152#define NAMELEN 16
fe5dba13 153\&
30a87638
MK
154static void *
155threadfunc(void *parm)
5df98ea9 156{
b615a97e 157 sleep(5); // allow main program to set the thread name
5df98ea9
MK
158 return NULL;
159}
fe5dba13 160\&
30a87638 161int
aa1f53cc 162main(int argc, char *argv[])
5df98ea9
MK
163{
164 pthread_t thread;
165 int rc;
166 char thread_name[NAMELEN];
fe5dba13 167\&
5df98ea9 168 rc = pthread_create(&thread, NULL, threadfunc, NULL);
b615a97e 169 if (rc != 0)
5a5208c1 170 errc(EXIT_FAILURE, rc, "pthread_create");
fe5dba13 171\&
5df98ea9 172 rc = pthread_getname_np(thread, thread_name, NAMELEN);
b615a97e 173 if (rc != 0)
5a5208c1 174 errc(EXIT_FAILURE, rc, "pthread_getname_np");
fe5dba13 175\&
d1a71985 176 printf("Created a thread. Default name is: %s\en", thread_name);
e49d4430 177 rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
b615a97e 178 if (rc != 0)
5a5208c1 179 errc(EXIT_FAILURE, rc, "pthread_setname_np");
fe5dba13 180\&
5df98ea9 181 sleep(2);
fe5dba13 182\&
44a16bcb 183 rc = pthread_getname_np(thread, thread_name, NAMELEN);
b615a97e 184 if (rc != 0)
5a5208c1 185 errc(EXIT_FAILURE, rc, "pthread_getname_np");
d1a71985 186 printf("The thread name after setting it is %s.\en", thread_name);
fe5dba13 187\&
5df98ea9 188 rc = pthread_join(thread, NULL);
b615a97e 189 if (rc != 0)
5a5208c1 190 errc(EXIT_FAILURE, rc, "pthread_join");
fe5dba13 191\&
d1a71985 192 printf("Done\en");
5df98ea9
MK
193 exit(EXIT_SUCCESS);
194}
e7d0bb47 195.EE
b0b6ab4e 196.\" SRC END
5df98ea9
MK
197.SH SEE ALSO
198.ad l
199.nh
200.BR prctl (2),
201.BR pthread_create (3),
202.BR pthreads (7)