1PTHREAD_SETNAME_NP(3) Linux Programmer's Manual PTHREAD_SETNAME_NP(3)
2
3
4
6 pthread_setname_np, pthread_getname_np - set/get the name of a thread
7
9 #define _GNU_SOURCE /* See feature_test_macros(7) */
10 #include <pthread.h>
11 int pthread_setname_np(pthread_t thread, const char *name);
12 int pthread_getname_np(pthread_t thread,
13 char *name, size_t len);
14
15 Compile and link with -pthread.
16
18 By default, all the threads created using pthread_create() inherit the
19 program name. The pthread_setname_np() function can be used to set a
20 unique name for a thread, which can be useful for debugging multi‐
21 threaded applications. The thread name is a meaningful C language
22 string, whose length is restricted to 16 characters, including the ter‐
23 minating null byte ('\0'). The thread argument specifies the thread
24 whose name is to be changed; name specifies the new name.
25
26 The pthread_getname_np() function can be used to retrieve the name of
27 the thread. The thread argument specifies the thread whose name is to
28 be retrieved. The buffer name is used to return the thread name; len
29 specifies the number of bytes available in name. The buffer specified
30 by name should be at least 16 characters in length. The returned
31 thread name in the output buffer will be null terminated.
32
34 On success, these functions return 0; on error, they return a nonzero
35 error number.
36
38 The pthread_setname_np() function can fail with the following error:
39
40 ERANGE The length of the string specified pointed to by name exceeds
41 the allowed limit.
42
43 The pthread_getname_np() function can fail with the following error:
44
45 ERANGE The buffer specified by name and len is too small to hold the
46 thread name.
47
48 If either of these functions fails to open /proc/self/task/[tid]/comm,
49 then the call may fail with one of the errors described in open(2).
50
52 These functions first appeared in glibc in version 2.12.
53
55 For an explanation of the terms used in this section, see
56 attributes(7).
57
58 ┌──────────────────────┬───────────────┬─────────┐
59 │Interface │ Attribute │ Value │
60 ├──────────────────────┼───────────────┼─────────┤
61 │pthread_setname_np(), │ Thread safety │ MT-Safe │
62 │pthread_getname_np() │ │ │
63 └──────────────────────┴───────────────┴─────────┘
64
66 These functions are nonstandard GNU extensions.
67
69 pthread_setname_np() internally writes to the thread-specific comm file
70 under the /proc filesystem: /proc/self/task/[tid]/comm. pthread_get‐
71 name_np() retrieves it from the same location.
72
74 The program below demonstrates the use of pthread_setname_np() and
75 pthread_getname_np().
76
77 The following shell session shows a sample run of the program:
78
79 $ ./a.out
80 Created a thread. Default name is: a.out
81 The thread name after setting it is THREADFOO.
82 ^Z # Suspend the program
83 [1]+ Stopped ./a.out
84 $ ps H -C a.out -o 'pid tid cmd comm'
85 PID TID CMD COMMAND
86 5990 5990 ./a.out a.out
87 5990 5991 ./a.out THREADFOO
88 $ cat /proc/5990/task/5990/comm
89 a.out
90 $ cat /proc/5990/task/5991/comm
91 THREADFOO
92
93 Program source
94
95 #define _GNU_SOURCE
96 #include <pthread.h>
97 #include <stdio.h>
98 #include <string.h>
99 #include <unistd.h>
100 #include <errno.h>
101 #include <stdlib.h>
102
103 #define NAMELEN 16
104
105 #define errExitEN(en, msg) \
106 do { errno = en; perror(msg); exit(EXIT_FAILURE); \
107 } while (0)
108
109 static void *
110 threadfunc(void *parm)
111 {
112 sleep(5); // allow main program to set the thread name
113 return NULL;
114 }
115
116 int
117 main(int argc, char **argv)
118 {
119 pthread_t thread;
120 int rc;
121 char thread_name[NAMELEN];
122
123 rc = pthread_create(&thread, NULL, threadfunc, NULL);
124 if (rc != 0)
125 errExitEN(rc, "pthread_create");
126
127 rc = pthread_getname_np(thread, thread_name, NAMELEN);
128 if (rc != 0)
129 errExitEN(rc, "pthread_getname_np");
130
131 printf("Created a thread. Default name is: %s\n", thread_name);
132 rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
133 if (rc != 0)
134 errExitEN(rc, "pthread_setname_np");
135
136 sleep(2);
137
138 rc = pthread_getname_np(thread, thread_name,
139 (argc > 2) ? atoi(argv[1]) : NAMELEN);
140 if (rc != 0)
141 errExitEN(rc, "pthread_getname_np");
142 printf("The thread name after setting it is %s.\n", thread_name);
143
144 rc = pthread_join(thread, NULL);
145 if (rc != 0)
146 errExitEN(rc, "pthread_join");
147
148 printf("Done\n");
149 exit(EXIT_SUCCESS);
150 }
151
153 prctl(2), pthread_create(3), pthreads(7)
154
156 This page is part of release 4.16 of the Linux man-pages project. A
157 description of the project, information about reporting bugs, and the
158 latest version of this page, can be found at
159 https://www.kernel.org/doc/man-pages/.
160
161
162
163Linux 2017-09-15 PTHREAD_SETNAME_NP(3)