man-pages/man3/pthread_setname_np.3.html
2021-03-31 01:06:50 +01:00

313 lines
9.4 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PTHREAD_SETNAME_NP</TITLE>
</HEAD><BODY>
<H1>PTHREAD_SETNAME_NP</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
pthread_setname_np, pthread_getname_np - set/get the name of a thread
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#define _GNU_SOURCE</B> /* See <A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A>(7) */
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</B>
<B>int pthread_setname_np(pthread_t </B><I>thread</I><B>, const char *</B><I>name</I><B>);</B>
<B>int pthread_getname_np(pthread_t </B><I>thread</I><B>,</B>
<B> char *</B><I>name</I><B>, size_t </B><I>len</I><B>);</B>
</PRE>
<P>
Compile and link with <I>-pthread</I>.
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
By default, all the threads created using
<B>pthread_create</B>()
inherit the program name.
The
<B>pthread_setname_np</B>()
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 ('\0').
The
<I>thread</I>
argument specifies the thread whose name is to be changed;
<I>name</I>
specifies the new name.
<P>
The
<B>pthread_getname_np</B>()
function can be used to retrieve the name of the thread.
The
<I>thread</I>
argument specifies the thread whose name is to be retrieved.
The buffer
<I>name</I>
is used to return the thread name;
<I>len</I>
specifies the number of bytes available in
<I>name</I>.
The buffer specified by
<I>name</I>
should be at least 16 characters in length.
The returned thread name in the output buffer will be null terminated.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return 0;
on error, they return a nonzero error number.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
The
<B>pthread_setname_np</B>()
function can fail with the following error:
<DL COMPACT>
<DT id="1"><B>ERANGE</B>
<DD>
The length of the string specified pointed to by
<I>name</I>
exceeds the allowed limit.
</DL>
<P>
The
<B>pthread_getname_np</B>()
function can fail with the following error:
<DL COMPACT>
<DT id="2"><B>ERANGE</B>
<DD>
The buffer specified by
<I>name</I>
and
<I>len</I>
is too small to hold the thread name.
</DL>
<P>
If either of these functions fails to open
<I>/proc/self/task/[tid]/comm</I>,
then the call may fail with one of the errors described in
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2).
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
These functions first appeared in glibc in version 2.12.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>pthread_setname_np</B>(),
<B>pthread_getname_np</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
These functions are nonstandard GNU extensions;
hence the suffix &quot;_np&quot; (nonportable) in the names.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
<B>pthread_setname_np</B>()
internally writes to the thread-specific
<I>comm</I>
file under the
<I>/proc</I>
filesystem:
<I>/proc/self/task/[tid]/comm</I>.
<B>pthread_getname_np</B>()
retrieves it from the same location.
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
<P>
The program below demonstrates the use of
<B>pthread_setname_np</B>()
and
<B>pthread_getname_np</B>().
<P>
The following shell session shows a sample run of the program:
<P>
$<B> ./a.out</B>
Created a thread. Default name is: a.out
The thread name after setting it is THREADFOO.
<B>^Z</B> # Suspend the program
[1]+ Stopped ./a.out
$ <B>ps H -C a.out -o 'pid tid cmd comm'</B>
<BR>&nbsp;&nbsp;PID&nbsp;&nbsp;&nbsp;TID&nbsp;CMD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;COMMAND
<BR>&nbsp;5990&nbsp;&nbsp;5990&nbsp;./a.out&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.out
<BR>&nbsp;5990&nbsp;&nbsp;5991&nbsp;./a.out&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;THREADFOO
$ <B>cat /proc/5990/task/5990/comm</B>
a.out
$ <B>cat /proc/5990/task/5991/comm</B>
THREADFOO
<A NAME="lbAL">&nbsp;</A>
<H3>Program source</H3>
#define _GNU_SOURCE
#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
#define NAMELEN 16
<P>
#define errExitEN(en, msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;errno&nbsp;=&nbsp;en;&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;\
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(0)
<P>
static void *
threadfunc(void *parm)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?5+sleep">sleep</A>(5);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;allow&nbsp;main&nbsp;program&nbsp;to&nbsp;set&nbsp;the&nbsp;thread&nbsp;name
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NULL;
}
<P>
int
main(int argc, char **argv)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thread;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;rc;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;thread_name[NAMELEN];
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;=&nbsp;pthread_create(&amp;thread,&nbsp;NULL,&nbsp;threadfunc,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(rc&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExitEN(rc,&nbsp;&quot;pthread_create&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;=&nbsp;pthread_getname_np(thread,&nbsp;thread_name,&nbsp;NAMELEN);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(rc&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExitEN(rc,&nbsp;&quot;pthread_getname_np&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Created&nbsp;a&nbsp;thread.&nbsp;Default&nbsp;name&nbsp;is:&nbsp;%s\n&quot;,&nbsp;thread_name);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;=&nbsp;pthread_setname_np(thread,&nbsp;(argc&nbsp;&gt;&nbsp;1)&nbsp;?&nbsp;argv[1]&nbsp;:&nbsp;&quot;THREADFOO&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(rc&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExitEN(rc,&nbsp;&quot;pthread_setname_np&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;=&nbsp;pthread_getname_np(thread,&nbsp;thread_name,
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(argc&nbsp;&gt;&nbsp;2)&nbsp;?&nbsp;atoi(argv[1])&nbsp;:&nbsp;NAMELEN);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(rc&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExitEN(rc,&nbsp;&quot;pthread_getname_np&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;The&nbsp;thread&nbsp;name&nbsp;after&nbsp;setting&nbsp;it&nbsp;is&nbsp;%s.\n&quot;,&nbsp;thread_name);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;rc&nbsp;=&nbsp;pthread_join(thread,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(rc&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errExitEN(rc,&nbsp;&quot;pthread_join&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Done\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?2+prctl">prctl</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="3"><A HREF="#lbAB">NAME</A><DD>
<DT id="4"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="5"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="6"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="7"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="8"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="9"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="10"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="11"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="12"><A HREF="#lbAK">EXAMPLE</A><DD>
<DL>
<DT id="13"><A HREF="#lbAL">Program source</A><DD>
</DL>
<DT id="14"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="15"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:53 GMT, March 31, 2021
</BODY>
</HTML>