466 lines
21 KiB
HTML
466 lines
21 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PTHREAD_GETATTR_NP</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PTHREAD_GETATTR_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_getattr_np - get attributes of created 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_getattr_np(pthread_t </B><I>thread</I><B>, pthread_attr_t *</B><I>attr</I><B>);</B>
|
|
|
|
Compile and link with <I>-pthread</I>.
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>pthread_getattr_np</B>()
|
|
|
|
function initializes the thread attributes object referred to by
|
|
<I>attr</I>
|
|
|
|
so that it contains actual attribute values describing the running thread
|
|
<I>thread</I>.
|
|
|
|
<P>
|
|
|
|
The returned attribute values may differ from
|
|
the corresponding attribute values passed in the
|
|
<I>attr</I>
|
|
|
|
object that was used to create the thread using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_create">pthread_create</A></B>(3).
|
|
|
|
In particular, the following attributes may differ:
|
|
<DL COMPACT>
|
|
<DT id="1">*<DD>
|
|
the detach state, since a joinable thread may have detached itself
|
|
after creation;
|
|
<DT id="2">*<DD>
|
|
the stack size,
|
|
which the implementation may align to a suitable boundary.
|
|
<DT id="3">*<DD>
|
|
and the guard size,
|
|
which the implementation may round upward to a multiple of the page size,
|
|
or ignore (i.e., treat as 0),
|
|
if the application is allocating its own stack.
|
|
</DL>
|
|
<P>
|
|
|
|
Furthermore, if the stack address attribute was not set
|
|
in the thread attributes object used to create the thread,
|
|
then the returned thread attributes object will report the actual
|
|
stack address that the implementation selected for the thread.
|
|
<P>
|
|
|
|
When the thread attributes object returned by
|
|
<B>pthread_getattr_np</B>()
|
|
|
|
is no longer required, it should be destroyed using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_destroy">pthread_attr_destroy</A></B>(3).
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, this function returns 0;
|
|
on error, it returns a nonzero error number.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="4"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
|
|
Insufficient memory.
|
|
</DL>
|
|
<P>
|
|
|
|
In addition, if
|
|
<I>thread</I>
|
|
|
|
refers to the main thread, then
|
|
<B>pthread_getattr_np</B>()
|
|
|
|
can fail because of errors from various underlying calls:
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3),
|
|
|
|
if
|
|
<I>/proc/self/maps</I>
|
|
|
|
can't be opened;
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+getrlimit">getrlimit</A></B>(2),
|
|
|
|
if the
|
|
<B>RLIMIT_STACK</B>
|
|
|
|
resource limit is not supported.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
This function is available in glibc since version 2.2.3.
|
|
<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_getattr_np</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
This function is a nonstandard GNU extension;
|
|
hence the suffix "_np" (nonportable) in the name.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>pthread_getattr_np</B>().
|
|
|
|
The program creates a thread that then uses
|
|
<B>pthread_getattr_np</B>()
|
|
|
|
to retrieve and display its guard size, stack address,
|
|
and stack size attributes.
|
|
Command-line arguments can be used to set these attributes
|
|
to values other than the default when creating the thread.
|
|
The shell sessions below demonstrate the use of the program.
|
|
<P>
|
|
|
|
In the first run, on an x86-32 system,
|
|
a thread is created using default attributes:
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ulimit -s</B> # No stack limit ==> default stack size is 2 MB
|
|
|
|
unlimited
|
|
$<B> ./a.out</B>
|
|
|
|
Attributes of created thread:
|
|
<BR> Guard size = 4096 bytes
|
|
<BR> Stack address = 0x40196000 (EOS = 0x40397000)
|
|
<BR> Stack size = 0x201000 (2101248) bytes
|
|
|
|
|
|
<P>
|
|
|
|
In the following run, we see that if a guard size is specified,
|
|
it is rounded up to the next multiple of the system page size
|
|
(4096 bytes on x86-32):
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out -g 4097</B>
|
|
|
|
Thread attributes object after initializations:
|
|
<BR> Guard size = 4097 bytes
|
|
<BR> Stack address = (nil)
|
|
<BR> Stack size = 0x0 (0) bytes
|
|
<P>
|
|
Attributes of created thread:
|
|
<BR> Guard size = 8192 bytes
|
|
<BR> Stack address = 0x40196000 (EOS = 0x40397000)
|
|
<BR> Stack size = 0x201000 (2101248) bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
In the last run, the program manually allocates a stack for the thread.
|
|
In this case, the guard size attribute is ignored.
|
|
<P>
|
|
|
|
|
|
|
|
$<B> ./a.out -g 4096 -s 0x8000 -a</B>
|
|
|
|
Allocated thread stack at 0x804d000
|
|
<P>
|
|
Thread attributes object after initializations:
|
|
<BR> Guard size = 4096 bytes
|
|
<BR> Stack address = 0x804d000 (EOS = 0x8055000)
|
|
<BR> Stack size = 0x8000 (32768) bytes
|
|
<P>
|
|
Attributes of created thread:
|
|
<BR> Guard size = 0 bytes
|
|
<BR> Stack address = 0x804d000 (EOS = 0x8055000)
|
|
<BR> Stack size = 0x8000 (32768) bytes
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
|
|
#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/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
#define handle_error_en(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
static void
|
|
display_stack_related_attributes(pthread_attr_t *attr, char *prefix)
|
|
{
|
|
<BR> int s;
|
|
<BR> size_t stack_size, guard_size;
|
|
<BR> void *stack_addr;
|
|
<P>
|
|
<BR> s = pthread_attr_getguardsize(attr, &guard_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getguardsize");
|
|
<BR> printf("%sGuard size = %d bytes\n", prefix, guard_size);
|
|
<P>
|
|
<BR> s = pthread_attr_getstack(attr, &stack_addr, &stack_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_getstack");
|
|
<BR> printf("%sStack address = %p", prefix, stack_addr);
|
|
<BR> if (stack_size > 0)
|
|
<BR> printf(" (EOS = %p)", (char *) stack_addr + stack_size);
|
|
<BR> printf("\n");
|
|
<BR> printf("%sStack size = 0x%x (%d) bytes\n",
|
|
<BR> prefix, stack_size, stack_size);
|
|
}
|
|
<P>
|
|
static void
|
|
display_thread_attributes(pthread_t thread, char *prefix)
|
|
{
|
|
<BR> int s;
|
|
<BR> pthread_attr_t attr;
|
|
<P>
|
|
<BR> s = pthread_getattr_np(thread, &attr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_getattr_np");
|
|
<P>
|
|
<BR> display_stack_related_attributes(&attr, prefix);
|
|
<P>
|
|
<BR> s = pthread_attr_destroy(&attr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_destroy");
|
|
}
|
|
<P>
|
|
static void * /* Start function for thread we create */
|
|
thread_start(void *arg)
|
|
{
|
|
<BR> printf("Attributes of created thread:\n");
|
|
<BR> display_thread_attributes(pthread_self(), "\t");
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS); /* Terminate all threads */
|
|
}
|
|
<P>
|
|
static void
|
|
usage(char *pname, char *msg)
|
|
{
|
|
<BR> if (msg != NULL)
|
|
<BR> fputs(msg, stderr);
|
|
<BR> fprintf(stderr, "Usage: %s [-s stack-size [-a]]"
|
|
<BR> " [-g guard-size]\n", pname);
|
|
<BR> fprintf(stderr, "\t\t-a means program should allocate stack\n");
|
|
<BR> exit(EXIT_FAILURE);
|
|
}
|
|
<P>
|
|
static pthread_attr_t * /* Get thread attributes from command line */
|
|
get_thread_attributes_from_cl(int argc, char *argv[],
|
|
<BR> pthread_attr_t *attrp)
|
|
{
|
|
<BR> int s, opt, allocate_stack;
|
|
<BR> long stack_size, guard_size;
|
|
<BR> void *stack_addr;
|
|
<BR> pthread_attr_t *ret_attrp = NULL; /* Set to attrp if we initialize
|
|
<BR> a thread attributes object */
|
|
<BR> allocate_stack = 0;
|
|
<BR> stack_size = -1;
|
|
<BR> guard_size = -1;
|
|
<P>
|
|
<BR> while ((opt = getopt(argc, argv, "ag:s:")) != -1) {
|
|
<BR> switch (opt) {
|
|
<BR> case 'a': allocate_stack = 1; break;
|
|
<BR> case 'g': guard_size = strtoul(optarg, NULL, 0); break;
|
|
<BR> case 's': stack_size = strtoul(optarg, NULL, 0); break;
|
|
<BR> default: usage(argv[0], NULL);
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> if (allocate_stack && stack_size == -1)
|
|
<BR> usage(argv[0], "Specifying -a without -s makes no sense\n");
|
|
<P>
|
|
<BR> if (argc > optind)
|
|
<BR> usage(argv[0], "Extraneous command-line arguments\n");
|
|
<P>
|
|
<BR> if (stack_size >= 0 || guard_size > 0) {
|
|
<BR> ret_attrp = attrp;
|
|
<P>
|
|
<BR> s = pthread_attr_init(attrp);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_init");
|
|
<BR> }
|
|
<P>
|
|
<BR> if (stack_size >= 0) {
|
|
<BR> if (!allocate_stack) {
|
|
<BR> s = pthread_attr_setstacksize(attrp, stack_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setstacksize");
|
|
<BR> } else {
|
|
<BR> s = posix_memalign(&stack_addr, sysconf(_SC_PAGESIZE),
|
|
<BR> stack_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "posix_memalign");
|
|
<BR> printf("Allocated thread stack at %p\n\n", stack_addr);
|
|
<P>
|
|
<BR> s = pthread_attr_setstack(attrp, stack_addr, stack_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setstacksize");
|
|
<BR> }
|
|
<BR> }
|
|
<P>
|
|
<BR> if (guard_size >= 0) {
|
|
<BR> s = pthread_attr_setguardsize(attrp, guard_size);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_setstacksize");
|
|
<BR> }
|
|
<P>
|
|
<BR> return ret_attrp;
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> int s;
|
|
<BR> pthread_t thr;
|
|
<BR> pthread_attr_t attr;
|
|
<BR> pthread_attr_t *attrp = NULL; /* Set to &attr if we initialize
|
|
<BR> a thread attributes object */
|
|
<P>
|
|
<BR> attrp = get_thread_attributes_from_cl(argc, argv, &attr);
|
|
<P>
|
|
<BR> if (attrp != NULL) {
|
|
<BR> printf("Thread attributes object after initializations:\n");
|
|
<BR> display_stack_related_attributes(attrp, "\t");
|
|
<BR> printf("\n");
|
|
<BR> }
|
|
<P>
|
|
<BR> s = pthread_create(&thr, attrp, &thread_start, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_create");
|
|
<P>
|
|
<BR> if (attrp != NULL) {
|
|
<BR> s = pthread_attr_destroy(attrp);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_attr_destroy");
|
|
<BR> }
|
|
<P>
|
|
<BR> pause(); /* Terminates when other thread calls exit() */
|
|
}
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getaffinity_np">pthread_attr_getaffinity_np</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getdetachstate">pthread_attr_getdetachstate</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getguardsize">pthread_attr_getguardsize</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getinheritsched">pthread_attr_getinheritsched</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getschedparam">pthread_attr_getschedparam</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getschedpolicy">pthread_attr_getschedpolicy</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getscope">pthread_attr_getscope</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getstack">pthread_attr_getstack</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getstackaddr">pthread_attr_getstackaddr</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_getstacksize">pthread_attr_getstacksize</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_attr_init">pthread_attr_init</A></B>(3),
|
|
|
|
<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="lbAM"> </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="5"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="10"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="11"><A HREF="#lbAH">ATTRIBUTES</A><DD>
|
|
<DT id="12"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="13"><A HREF="#lbAJ">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="14"><A HREF="#lbAK">Program source</A><DD>
|
|
</DL>
|
|
<DT id="15"><A HREF="#lbAL">SEE ALSO</A><DD>
|
|
<DT id="16"><A HREF="#lbAM">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>
|