1PTHREAD_SETNAME_NP(3)      Linux Programmer's Manual     PTHREAD_SETNAME_NP(3)
2
3
4

NAME

6       pthread_setname_np, pthread_getname_np - set/get the name of a thread
7

SYNOPSIS

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                              const char *name, size_t len);
14
15       Compile and link with -pthread.
16

DESCRIPTION

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

RETURN VALUE

34       On success, these functions return 0; on error, they return  a  nonzero
35       error number.
36

ERRORS

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

VERSIONS

52       These functions first appeared in glibc in version 2.12.
53

CONFORMING TO

55       These functions are nonstandard GNU extensions.
56

NOTES

58       pthread_setname_np() internally writes to the thread specific comm file
59       under   /proc   filesystem:  /proc/self/task/[tid]/comm.   pthread_get‐
60       name_np() retrieves it from the same location.
61

EXAMPLE

63       The program below demonstrates  the  use  of  pthread_setname_np()  and
64       pthread_getname_np().
65
66       The following shell session shows a sample run of the program:
67
68           $ ./a.out
69           Created a thread. Default name is: a.out
70           The thread name after setting it is THREADFOO.
71           ^Z                           # Suspend the program
72           [1]+  Stopped           ./a.out
73           $ ps H -C a.out -o 'pid tid cmd comm'
74             PID   TID CMD                         COMMAND
75            5990  5990 ./a.out                     a.out
76            5990  5991 ./a.out                     THREADFOO
77           $ cat /proc/5990/task/5990/comm
78           a.out
79           $ cat /proc/5990/task/5991/comm
80           THREADFOO
81
82   Program source
83
84       #define _GNU_SOURCE
85       #include <pthread.h>
86       #include <stdio.h>
87       #include <string.h>
88       #include <unistd.h>
89       #include <errno.h>
90       #include <stdlib.h>
91
92       #define NAMELEN 16
93
94       #define errExitEN(en, msg) \
95                   do { errno = en; perror(msg); exit(EXIT_FAILURE); \
96               } while (0)
97
98       static void *
99       threadfunc(void *parm)
100       {
101           sleep(5);          // allow main program to set the thread name
102           return NULL;
103       }
104
105       int
106       main(int argc, char **argv)
107       {
108           pthread_t thread;
109           int rc;
110           char thread_name[NAMELEN];
111
112           rc = pthread_create(&thread, NULL, threadfunc, NULL);
113           if (rc != 0)
114               errExitEN(rc, "pthread_create");
115
116           rc = pthread_getname_np(thread, thread_name, NAMELEN);
117           if (rc != 0)
118               errExitEN(rc, "pthread_getname_np");
119
120           printf("Created a thread. Default name is: %s\n", thread_name);
121           rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
122           if (rc != 0)
123               errExitEN(rc, "pthread_setname_np");
124
125           sleep(2);
126
127           rc = pthread_getname_np(thread, thread_name,
128                                   (argc > 2) ? atoi(argv[1]) : NAMELEN);
129           if (rc != 0)
130               errExitEN(rc, "pthread_getname_np");
131           printf("The thread name after setting it is %s.\n", thread_name);
132
133           rc = pthread_join(thread, NULL);
134           if (rc != 0)
135               errExitEN(rc, "pthread_join");
136
137           printf("Done\n");
138           exit(EXIT_SUCCESS);
139       }
140

SEE ALSO

142       prctl(2), pthread_create(3), pthreads(7)
143

COLOPHON

145       This page is part of release 3.53 of the Linux man-pages project.  A
146       description of the project, and information about reporting bugs, can
147       be found at http://www.kernel.org/doc/man-pages/.
148
149
150
151Linux                             2013-06-21             PTHREAD_SETNAME_NP(3)
Impressum