]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_setname_np.3
pthread_setname_np.3: Rework ERRORS text layout
[thirdparty/man-pages.git] / man3 / pthread_setname_np.3
CommitLineData
5df98ea9
MK
1.\" Copyright (C) 2012 Chandan Apsangi <chandan.jc@gmail.com>
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
11.\"
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
19.\"
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
e4a83b01 23.TH PTHREAD_SETNAME_NP 3 2013-02-04 "Linux" "Linux Programmer's Manual"
5df98ea9
MK
24.SH NAME
25pthread_setname_np, pthread_getname_np \- set/get the name of a thread
26.SH SYNOPSIS
27.nf
28.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
29.B #include <pthread.h>
30.BI "int pthread_setname_np(pthread_t *" thread ", const char *" name ");
e4a83b01
MK
31.BI "int pthread_getname_np(pthread_t *" thread ,
32.BI " const char *" name ", size_t " len );
5df98ea9
MK
33.fi
34.sp
35Compile and link with \fI\-pthread\fP.
36.SH DESCRIPTION
37By default, all the threads created using
e4a83b01 38.BR pthread_create ()
5df98ea9 39inherit the program name.
e4a83b01
MK
40The
41.BR pthread_setname_np ()
42fuction can be used to set a unique name for a thread,
43which can be useful for debugging
5df98ea9
MK
44multi-threaded applications.
45The thread name is a meaningful C language string, whose length is
ba95a530 46restricted to 16 characters, including the terminating null byte.
6350e974
MK
47The
48.I thread
49argument specifies the thread whose name is to be changed;
50.I name
51specifies the new name.
5df98ea9
MK
52
53The
e4a83b01
MK
54.BR pthread_getname_np ()
55function can be used to retrieve the name of the thread.
6350e974
MK
56The
57.I thread
58argument specifies the thread whose name is to be retriecved.
59The buffer
60.I name
61is used to return the thread name;
62.I len
63specifies the number of bytes available in
64.IR name .
e4a83b01
MK
65The buffer specified by
66.I name
6350e974 67should be at least 16 characters in length.
5df98ea9
MK
68The returned thread name in the output buffer will be null terminated.
69.SH RETURN VALUE
70On success, these functions return 0;
71on error, they return a nonzero error number.
72.SH ERRORS
eb6c2cc6
MK
73The
74.BR pthread_setname_np ()
75function can fail with the following errors:
5df98ea9
MK
76.TP
77.B ERANGE
e4a83b01
MK
78The length of the string specified pointed to by
79.I name
80exceeds the allowed limit.
81.PP
82If either of these functions fails to open
83.IR /proc/self/task/[tid]/comm ,
84then the call may fail with one of the errors described in
85.BR open (2).
fbc7552a
MK
86.SH VERSIONS
87These functions first appeared in glibc in version 2.12.
c2ba212d
MK
88.SH CONFORMING TO
89These functions are nonstandard GNU extensions.
5df98ea9 90.SH NOTES
e4a83b01 91.BR pthread_setname_np ()
5df98ea9
MK
92internally writes to the thread specific comm file under
93.IR /proc
94filesystem:
e4a83b01
MK
95.IR /proc/self/task/[tid]/comm .
96.BR pthread_getname_np ()
5df98ea9
MK
97retreives it from the same location.
98
99.SH EXAMPLE
100.PP
101The program below demonstrates the use of
e4a83b01
MK
102.BR pthread_setname_np ()
103and
5df98ea9
MK
104.BR pthread_getname_np ().
105
106The following shell session shows a sample run of the program:
107.in +4n
108.nf
109
e4a83b01 110.RB "$" " ./a.out"
5df98ea9
MK
111Created a thread. Default name is: a.out
112The thread name after setting it is THREADFOO.
6cfa298b
MK
113\fB^Z\fP # Suspend the program
114[1]+ Stopped ./a.out
115.RB "$ " "ps H -C a.out -o 'pid tid cmd comm'"
116 PID TID CMD COMMAND
117 5990 5990 ./a.out a.out
118 5990 5991 ./a.out THREADFOO
119.RB "$ " "cat /proc/5990/task/5990/comm"
120a.out
121.RB "$ " "cat /proc/5990/task/5991/comm"
122THREADFOO
5df98ea9
MK
123.fi
124.in
125
126.SS Program source
127\&
128.nf
129#define _GNU_SOURCE
5df98ea9
MK
130#include <pthread.h>
131#include <stdio.h>
132#include <string.h>
133#include <unistd.h>
134#include <errno.h>
135#include <stdlib.h>
136
137#define NAMELEN 16
138
139#define errExitEN(en, msg) \\
b615a97e 140 do { errno = en; perror(msg); exit(EXIT_FAILURE); \\
5df98ea9
MK
141 } while (0)
142
30a87638
MK
143static void *
144threadfunc(void *parm)
5df98ea9 145{
b615a97e 146 sleep(5); // allow main program to set the thread name
5df98ea9
MK
147 return NULL;
148}
149
30a87638
MK
150int
151main(int argc, char **argv)
5df98ea9
MK
152{
153 pthread_t thread;
154 int rc;
155 char thread_name[NAMELEN];
156
157 rc = pthread_create(&thread, NULL, threadfunc, NULL);
b615a97e
MK
158 if (rc != 0)
159 errExitEN(rc, "pthread_create");
e4a83b01 160
5df98ea9 161 rc = pthread_getname_np(thread, thread_name, NAMELEN);
b615a97e
MK
162 if (rc != 0)
163 errExitEN(rc, "pthread_getname_np");
91e38d87 164
b615a97e 165 printf("Created a thread. Default name is: %s\\n", thread_name);
e49d4430 166 rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
b615a97e
MK
167 if (rc != 0)
168 errExitEN(rc, "pthread_setname_np");
e4a83b01 169
5df98ea9 170 sleep(2);
b615a97e 171
101d0a57
MK
172 rc = pthread_getname_np(thread, thread_name,
173 (argc > 2) ? atoi(argv[1]) : NAMELEN);
b615a97e
MK
174 if (rc != 0)
175 errExitEN(rc, "pthread_getname_np");
5df98ea9 176 printf("The thread name after setting it is %s.\\n", thread_name);
b615a97e 177
5df98ea9 178 rc = pthread_join(thread, NULL);
b615a97e
MK
179 if (rc != 0)
180 errExitEN(rc, "pthread_join");
181
5df98ea9
MK
182 printf("Done\\n");
183 exit(EXIT_SUCCESS);
184}
185.fi
186.SH SEE ALSO
187.ad l
188.nh
189.BR prctl (2),
190.BR pthread_create (3),
191.BR pthreads (7)