Manual Pages for Linux CentOS command on man pthread_getattr_np
MyWebUniversity

Manual Pages for Linux CentOS command on man pthread_getattr_np

PTHREADGETATTRNP(3) Linux Programmer's Manual PTHREADGETATTRNP(3)

NAME

pthreadgetattrnp - get attributes of created thread SYNOPSIS

#define GNUSOURCE /* See featuretestmacros(7) */

#include int pthreadgetattrnp(pthreadt thread, pthreadattrt *attr);

Compile and link with -pthread. DESCRIPTION The pthreadgetattrnp() function initializes the thread attributes object referred to by attr so that it contains actual attribute values describing the running thread thread. The returned attribute values may differ from the corresponding attribute values passed in the attr object that was used to create the thread using pthreadcreate(3). In particular, the following attributes may differ: * the detach state, since a joinable thread may have detached itself after creation; * the stack size, which the implementation may align to a suitable boundary. * 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. 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 impleā€ mentation selected for the thread. When the thread attributes object returned by pthreadgetattrnp() is no longer required, it should be destroyed using pthreadattrdestroy(3). RETURN VALUE On success, this function returns 0; on error, it returns a nonzero error number. ERRORS ENOMEM Insufficient memory. In addition, if thread refers to the main thread, then pthreadgetattrnp() can fail because of errors from various underlying calls: fopen(3), if /proc/self/maps can't be opened; and getrlimit(2), if the RLIMITSTACK resource limit is not supported. VERSIONS This function is available in glibc since version 2.2.3. CONFORMING TO This function is a nonstandard GNU extension; hence the suffix "np" (nonportable) in the name. EXAMPLE The program below demonstrates the use of pthreadgetattrnp(). The program creates a thread that then uses pthreadgetattrnp() 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.

In the first run, on an x86-32 system, a thread is created using default attributes:

$ ulimit -s # No stack limit ==> default stack size is 2MB unlimited

$ ./a.out Attributes of created thread: Guard size = 4096 bytes Stack address = 0x40196000 (EOS = 0x40397000) Stack size = 0x201000 (2101248) bytes 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):

$ ./a.out -g 4097 Thread attributes object after initializations: Guard size = 4097 bytes Stack address = (nil) Stack size = 0x0 (0) bytes Attributes of created thread: Guard size = 8192 bytes Stack address = 0x40196000 (EOS = 0x40397000) Stack size = 0x201000 (2101248) bytes In the last run, the program manually allocates a stack for the thread. In this case, the guard size attribute is ignored.

$ ./a.out -g 4096 -s 0x8000 -a Allocated thread stack at 0x804d000 Thread attributes object after initializations: Guard size = 4096 bytes Stack address = 0x804d000 (EOS = 0x8055000) Stack size = 0x8000 (32768) bytes Attributes of created thread: Guard size = 0 bytes Stack address = 0x804d000 (EOS = 0x8055000) Stack size = 0x8000 (32768) bytes Program source

#define GNUSOURCE /* To get pthreadgetattrnp() declaration */

#include

#include

#include

#include

#include

#define handleerroren(en, msg) \ do { errno = en; perror(msg); exit(EXITFAILURE); } while (0) static void displaystackrelatedattributes(pthreadattrt *attr, char *prefix) { int s; sizet stacksize, guardsize; void *stackaddr; s = pthreadattrgetguardsize(attr, &guardsize); if (s != 0) handleerroren(s, "pthreadattrgetguardsize");

printf("%sGuard size = %d bytes\n", prefix, guardsize); s = pthreadattrgetstack(attr, &stackaddr, &stacksize); if (s != 0) handleerroren(s, "pthreadattrgetstack");

printf("%sStack address = %p", prefix, stackaddr); if (stacksize > 0)

printf(" (EOS = %p)", (char *) stackaddr + stacksize); printf("\n");

printf("%sStack size = 0x%x (%d) bytes\n", prefix, stacksize, stacksize); } static void displaythreadattributes(pthreadt thread, char *prefix) { int s; pthreadattrt attr; s = pthreadgetattrnp(thread, &attr); if (s != 0) handleerroren(s, "pthreadgetattrnp"); displaystackrelatedattributes(&attr, prefix); s = pthreadattrdestroy(&attr); if (s != 0) handleerroren(s, "pthreadattrdestroy"); } static void * /* Start function for thread we create */ threadstart(void *arg) { printf("Attributes of created thread:\n"); displaythreadattributes(pthreadself(), "\t"); exit(EXITSUCCESS); /* Terminate all threads */ } static void usage(char *pname, char *msg) { if (msg != NULL) fputs(msg, stderr);

fprintf(stderr, "Usage: %s [-s stack-size [-a]]"

" [-g guard-size]\n", pname);

fprintf(stderr, "\t\t-a means program should allocate stack\n"); exit(EXITFAILURE); } static pthreadattrt * /* Get thread attributes from command line */ getthreadattributesfromcl(int argc, char *argv[], pthreadattrt *attrp) { int s, opt, allocatestack; long stacksize, guardsize; void *stackaddr; pthreadattrt *retattrp = NULL; /* Set to attrp if we initialize a thread attributes object */ allocatestack = 0;

stacksize = -1;

guardsize = -1;

while ((opt = getopt(argc, argv, "ag:s:")) != -1) { switch (opt) { case 'a': allocatestack = 1; break; case 'g': guardsize = strtoul(optarg, NULL, 0); break; case 's': stacksize = strtoul(optarg, NULL, 0); break; default: usage(argv[0], NULL); } }

if (allocatestack && stacksize == -1)

usage(argv[0], "Specifying -a without -s makes no sense\n"); if (argc > optind)

usage(argv[0], "Extraneous command-line arguments\n"); if (stacksize >= 0 || guardsize > 0) { retattrp = attrp; s = pthreadattrinit(attrp); if (s != 0) handleerroren(s, "pthreadattrinit"); } if (stacksize >= 0) { if (!allocatestack) { s = pthreadattrsetstacksize(attrp, stacksize); if (s != 0) handleerroren(s, "pthreadattrsetstacksize"); } else { s = posixmemalign(&stackaddr, sysconf(SCPAGESIZE), stacksize); if (s != 0) handleerroren(s, "posixmemalign");

printf("Allocated thread stack at %p\n\n", stackaddr); s = pthreadattrsetstack(attrp, stackaddr, stacksize); if (s != 0) handleerroren(s, "pthreadattrsetstacksize"); } } if (guardsize >= 0) { s = pthreadattrsetguardsize(attrp, guardsize); if (s != 0) handleerroren(s, "pthreadattrsetstacksize"); } return retattrp; } int main(int argc, char *argv[]) { int s; pthreadt thr; pthreadattrt attr; pthreadattrt *attrp = NULL; /* Set to &attr if we initialize a thread attributes object */ attrp = getthreadattributesfromcl(argc, argv, &attr); if (attrp != NULL) { printf("Thread attributes object after initializations:\n"); displaystackrelatedattributes(attrp, "\t"); printf("\n"); } s = pthreadcreate(&thr, attrp, &threadstart, NULL); if (s != 0) handleerroren(s, "pthreadcreate"); if (attrp != NULL) { s = pthreadattrdestroy(attrp); if (s != 0) handleerroren(s, "pthreadattrdestroy"); } pause(); /* Terminates when other thread calls exit() */ } SEE ALSO pthreadattrgetaffinitynp(3), pthreadattrgetdetachstate(3), pthreadattrgetguardsize(3), pthreadattrgetinheritsched(3), pthreadattrgetschedparam(3), pthreadattrgetschedpolicy(3), pthreadattrgetscope(3), pthreadattrgetstack(3), pthreadattrgetstackaddr(3), pthreadattrgetstacksize(3), pthreadattrinit(3), pthreadcreate(3), pthreads(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2010-09-10 PTHREADGETATTRNP(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™