1 PTHREAD_ATTR_GET_NP(3C) Standard C Library Functions PTHREAD_ATTR_GET_NP(3C) 2 3 NAME 4 pthread_attr_get_np - get pthread attributes of a running thread 5 6 SYNOPSIS 7 #include <pthread.h> 8 9 int 10 pthread_attr_get_np(pthread_t thread, pthread_attr_t *attr); 11 12 DESCRIPTION 13 The pthread_attr_get_np() function provides a way to get the attributes 14 of the thread thread after it has been created. This function is most 15 commonly used to obtain the actual location and size of a thread's stack. 16 17 The attributes pointer, attr, will be filled in with the current 18 attributes for the thread. The attributes should be allocated by a call 19 to pthread_attr_init(3C) prior to calling the pthread_attr_get_np() 20 function. When attr is done being used, it should be destroyed through a 21 call to pthread_attr_destroy(3C). 22 23 The attributes of the thread thread will be the same as those passed in 24 at the time pthread_create(3C) was called (or the default set if none 25 were specified), except that the following values will be updated: 26 27 Thread Stack Size 28 If no explicit stack size was specified, then attr will contain 29 the actual size of the stack. 30 31 If the size of the stack was specified, then it may have been 32 changed to ensure that the required alignment of the platform is 33 satisfied. 34 35 The Stack Address 36 If no stack address was specified, then attr will contain the 37 actual address of the stack that the system allocated for the 38 thread. 39 40 Thread Detach State 41 The detach state, whether or not the thread may be joined by a 42 call to pthread_join(3C), may have changed since the process was 43 created due to a call to pthread_detach(3C). attr will reflect 44 the current setting of thread. 45 46 Thread Scheduling Parameter 47 The scheduling parameter attribute will be updated with the 48 current scheduling parameter of thread. This is the same 49 information as available through pthread_getschedparam(3C) and it 50 is the preferred interface for obtaining that information. 51 52 Thread Scheduling Policy 53 The scheduling policy attribute of attr will be updated with the 54 current scheduling policy being applied to the thread. This may 55 have changed, for example, due to a call to 56 pthread_setschedparam(3C). As with the thread's scheduling 57 parameter, the preferred interface for obtaining this information 58 is by using pthread_getschedparam(3C). 59 60 Thread Guard Size 61 The value of the guard size attribute for the thread will be 62 updated to reflect the actual size of the guard installed for 63 thread. For more information on the guard size of a thread and 64 its purpose, see pthread_attr_getguardsize(3C). 65 66 RETURN VALUES 67 Upon successful completion, the pthread_attr_get_np() and 68 pthread_getattr_np() functions return 0. Otherwise, an error number is 69 returned to indicate the error. 70 71 EXAMPLES 72 The following program demonstrates how to use these functions to get the 73 location and stack size of a newly created thread. 74 75 #include <assert.h> 76 #include <errno.h> 77 #include <pthread.h> 78 #include <stdio.h> 79 #include <stdlib.h> 80 #include <string.h> 81 82 static pthread_t g_thr; 83 84 void * 85 print_stackinfo(void *arg) 86 { 87 int ret; 88 pthread_attr_t attr; 89 pthread_t *thrp = arg; 90 void *stk; 91 size_t stksize; 92 93 if (pthread_attr_init(&attr) != 0) { 94 fprintf(stderr, "failed to init attr: %s\n", 95 strerror(errno)); 96 exit(1); 97 } 98 99 if (pthread_attr_get_np(*thrp, &attr) != 0) { 100 fprintf(stderr, "failed to get thread attributes: %s\n", 101 strerror(errno)); 102 exit(1); 103 } 104 105 ret = pthread_attr_getstackaddr(&attr, &stk); 106 assert(ret == 0); 107 ret = pthread_attr_getstacksize(&attr, &stksize); 108 assert(ret == 0); 109 (void) printf("stack base is at %p, it is %d bytes large\n", 110 stk, stksize); 111 return (NULL); 112 } 113 114 int 115 main(void) 116 { 117 int ret; 118 119 if ((ret = pthread_create(&g_thr, NULL, print_stackinfo, 120 &g_thr) != 0)) { 121 fprintf(stderr, "failed to create a thread: %s\n", 122 strerror(errno)); 123 exit(1); 124 } 125 126 pthread_join(g_thr, NULL); 127 return (0); 128 } 129 130 ERRORS 131 The pthread_attr_get_np() function will fail if: 132 133 EINVAL The pthread_attr_t object attr was not properly 134 initialized with a call to pthread_attr_init(3C). 135 136 ESRCH No thread could be found corresponding to the 137 specified thread ID, thread. 138 139 INTERFACE STABILITY 140 Committed 141 142 MT-LEVEL 143 MT-Safe 144 145 SEE ALSO 146 pthread_attr_destroy(3C), pthread_attr_getdetachstate(3C), 147 pthread_attr_getguardsize(3C), pthread_attr_getinheritsched(3C), 148 pthread_attr_getschedparam(3C), pthread_attr_getschedpolicy(3C), 149 pthread_attr_getscope(3C), pthread_attr_getstackaddr(3C), 150 pthread_attr_getstacksize(3C), pthread_attr_init(3C), pthread_create(3C), 151 pthread_detach(3C), pthread_getschedparam(3C), pthread_setschedparam(3C), 152 attributes(5), threads(5) 153 154 illumos August 20, 2019 illumos