313 lines
9.4 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pthread_setname_np, pthread_getname_np - set/get the name of a thread
|
|
<A NAME="lbAC"> </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 <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></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"> </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"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these functions return 0;
|
|
on error, they return a nonzero error number.
|
|
<A NAME="lbAF"> </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"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
These functions first appeared in glibc in version 2.12.
|
|
<A NAME="lbAH"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
These functions are nonstandard GNU extensions;
|
|
hence the suffix "_np" (nonportable) in the names.
|
|
<A NAME="lbAJ"> </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"> </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> PID TID CMD COMMAND
|
|
<BR> 5990 5990 ./a.out a.out
|
|
<BR> 5990 5991 ./a.out THREADFOO
|
|
$ <B>cat /proc/5990/task/5990/comm</B>
|
|
|
|
a.out
|
|
$ <B>cat /proc/5990/task/5991/comm</B>
|
|
|
|
THREADFOO
|
|
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
#define NAMELEN 16
|
|
<P>
|
|
#define errExitEN(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); \
|
|
<BR> } while (0)
|
|
<P>
|
|
static void *
|
|
threadfunc(void *parm)
|
|
{
|
|
<BR> <A HREF="/cgi-bin/man/man2html?5+sleep">sleep</A>(5); // allow main program to set the thread name
|
|
<BR> return NULL;
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
<BR> pthread_t thread;
|
|
<BR> int rc;
|
|
<BR> char thread_name[NAMELEN];
|
|
<P>
|
|
<BR> rc = pthread_create(&thread, NULL, threadfunc, NULL);
|
|
<BR> if (rc != 0)
|
|
<BR> errExitEN(rc, "pthread_create");
|
|
<P>
|
|
<BR> rc = pthread_getname_np(thread, thread_name, NAMELEN);
|
|
<BR> if (rc != 0)
|
|
<BR> errExitEN(rc, "pthread_getname_np");
|
|
<P>
|
|
<BR> printf("Created a thread. Default name is: %s\n", thread_name);
|
|
<BR> rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
|
|
<BR> if (rc != 0)
|
|
<BR> errExitEN(rc, "pthread_setname_np");
|
|
<P>
|
|
<BR> <A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2);
|
|
<P>
|
|
<BR> rc = pthread_getname_np(thread, thread_name,
|
|
<BR> (argc > 2) ? atoi(argv[1]) : NAMELEN);
|
|
<BR> if (rc != 0)
|
|
<BR> errExitEN(rc, "pthread_getname_np");
|
|
<BR> printf("The thread name after setting it is %s.\n", thread_name);
|
|
<P>
|
|
<BR> rc = pthread_join(thread, NULL);
|
|
<BR> if (rc != 0)
|
|
<BR> errExitEN(rc, "pthread_join");
|
|
<P>
|
|
<BR> printf("Done\n");
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAM"> </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"> </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"> </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>
|