summaryrefslogtreecommitdiff
path: root/man3/pthread_setname_np.3
blob: 32492402a4aebe192a5b35a3229ee915df89ff8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
.\" Copyright (C) 2012 Chandan Apsangi <chandan.jc@gmail.com>
.\" and Copyright (C) 2013 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" %%%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
.\"
.TH PTHREAD_SETNAME_NP 3 2013-06-21 "Linux" "Linux Programmer's Manual"
.SH NAME
pthread_setname_np, pthread_getname_np \- set/get the name of a thread
.SH SYNOPSIS
.nf
.BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
.B #include <pthread.h>
.BI "int pthread_setname_np(pthread_t " thread ", const char *" name ");
.BI "int pthread_getname_np(pthread_t " thread ,
.BI "                       const char *" name ", size_t " len );
.fi
.sp
Compile and link with \fI\-pthread\fP.
.SH DESCRIPTION
By default, all the threads created using
.BR pthread_create ()
inherit the program name.
The
.BR pthread_setname_np ()
function can be used to set a unique name for a thread,
which can be useful for debugging
multithreaded applications.
The thread name is a meaningful C language string, whose length is
restricted to 16 characters, including the terminating null byte (\(aq\\0\(aq).
The
.I thread
argument specifies the thread whose name is to be changed;
.I name
specifies the new name.

The
.BR pthread_getname_np ()
function can be used to retrieve the name of the thread.
The
.I thread
argument specifies the thread whose name is to be retrieved.
The buffer
.I name
is used to return the thread name;
.I len
specifies the number of bytes available in
.IR name .
The buffer specified by
.I name
should be at least 16 characters in length.
The returned thread name in the output buffer will be null terminated.
.SH RETURN VALUE
On success, these functions return 0;
on error, they return a nonzero error number.
.SH ERRORS
The
.BR pthread_setname_np ()
function can fail with the following error:
.TP
.B ERANGE
The length of the string specified pointed to by
.I name
exceeds the allowed limit.
.PP
The
.BR pthread_getname_np ()
function can fail with the following error:
.TP
.B ERANGE
The buffer specified by
.I name
and
.I len
is too small to hold the thread name.
.PP
If either of these functions fails to open
.IR /proc/self/task/[tid]/comm ,
then the call may fail with one of the errors described in
.BR open (2).
.SH VERSIONS
These functions first appeared in glibc in version 2.12.
.SH CONFORMING TO
These functions are nonstandard GNU extensions.
.SH NOTES
.BR pthread_setname_np ()
internally writes to the thread specific comm file under
.IR /proc
filesystem:
.IR /proc/self/task/[tid]/comm .
.BR pthread_getname_np ()
retrieves it from the same location.
.SH EXAMPLE
.PP
The program below demonstrates the use of
.BR pthread_setname_np ()
and
.BR pthread_getname_np ().

The following shell session shows a sample run of the program:
.in +4n
.nf

.RB "$" " ./a.out"
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
.RB "$ " "ps H -C a.out -o 'pid tid cmd comm'"
  PID   TID CMD                         COMMAND
 5990  5990 ./a.out                     a.out
 5990  5991 ./a.out                     THREADFOO
.RB "$ " "cat /proc/5990/task/5990/comm"
a.out
.RB "$ " "cat /proc/5990/task/5991/comm"
THREADFOO
.fi
.in
.SS Program source
\&
.nf
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>

#define NAMELEN 16

#define errExitEN(en, msg) \\
            do { errno = en; perror(msg); exit(EXIT_FAILURE); \\
        } 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\\n", 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.\\n", thread_name);

    rc = pthread_join(thread, NULL);
    if (rc != 0)
        errExitEN(rc, "pthread_join");

    printf("Done\\n");
    exit(EXIT_SUCCESS);
}
.fi
.SH SEE ALSO
.ad l
.nh
.BR prctl (2),
.BR pthread_create (3),
.BR pthreads (7)