.\" 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.
.\"
.\" 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-02-04 "Linux" "Linux Programmer's Manual"
+.TH PTHREAD_SETNAME_NP 3 2019-03-06 "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 );
+.BI "int pthread_setname_np(pthread_t " thread ", const char *" name ");
+.BI "int pthread_getname_np(pthread_t " thread ,
+.BI " char *" name ", size_t " len );
.fi
-.sp
+.PP
Compile and link with \fI\-pthread\fP.
.SH DESCRIPTION
By default, all the threads created using
inherit the program name.
The
.BR pthread_setname_np ()
-fuction can be used to set a unique name for a thread,
+function can be used to set a unique name for a thread,
which can be useful for debugging
-multi-threaded applications.
+multithreaded applications.
The thread name is a meaningful C language string, whose length is
-restricted to 16 characters, including the terminating null byte.
-
+restricted to 16 characters, including the terminating null byte (\(aq\e0\(aq).
+The
+.I thread
+argument specifies the thread whose name is to be changed;
+.I name
+specifies the new name.
+.PP
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
-must be at least 16 characters in length.
+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
-.RB ( pthread_setname_np ())
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 ATTRIBUTES
+For an explanation of the terms used in this section, see
+.BR attributes (7).
+.TS
+allbox;
+lbw21 lb lb
+l l l.
+Interface Attribute Value
+T{
+.BR pthread_setname_np (),
+.BR pthread_getname_np ()
+T} Thread safety MT-Safe
+.TE
+.sp 1
+.SH CONFORMING TO
+These functions are nonstandard GNU extensions;
+hence the suffix "_np" (nonportable) in the names.
.SH NOTES
.BR pthread_setname_np ()
-internally writes to the thread specific comm file under
+internally writes to the thread-specific
+.I comm
+file under the
.IR /proc
filesystem:
.IR /proc/self/task/[tid]/comm .
.BR pthread_getname_np ()
-retreives it from the same location.
-
-.SH EXAMPLE
+retrieves it from the same location.
+.SH EXAMPLES
.PP
The program below demonstrates the use of
.BR pthread_setname_np ()
and
.BR pthread_getname_np ().
-
+.PP
The following shell session shows a sample run of the program:
+.PP
.in +4n
-.nf
-
+.EX
.RB "$" " ./a.out"
Created a thread. Default name is: a.out
The thread name after setting it is THREADFOO.
-Done
-.fi
+\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
+.EE
.in
-
.SS Program source
\&
-.nf
+.EX
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#define NAMELEN 16
-#define errExitEN(en, msg) \\
- do { errno = en; perror(msg); exit(EXIT_FAILURE); \\
+#define errExitEN(en, msg) \e
+ do { errno = en; perror(msg); exit(EXIT_FAILURE); \e
} while (0)
-static void *threadfunc(void *parm)
+static void *
+threadfunc(void *parm)
{
sleep(5); // allow main program to set the thread name
return NULL;
}
-int main(int argc, char **argv)
+int
+main(int argc, char **argv)
{
pthread_t thread;
int rc;
if (rc != 0)
errExitEN(rc, "pthread_getname_np");
- printf("Created a thread. Default name is: %s\\n", thread_name);
+ printf("Created a thread. Default name is: %s\en", 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, NAMELEN);
+
+ 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);
-
+ printf("The thread name after setting it is %s.\en", thread_name);
+
rc = pthread_join(thread, NULL);
if (rc != 0)
errExitEN(rc, "pthread_join");
-
- printf("Done\\n");
+
+ printf("Done\en");
exit(EXIT_SUCCESS);
}
-
-.fi
+.EE
.SH SEE ALSO
.ad l
.nh