]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/pthread_getattr_np.3
Many pages: Use correct letter case in page titles (TH)
[thirdparty/man-pages.git] / man3 / pthread_getattr_np.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH pthread_getattr_np 3 (date) "Linux man-pages (unreleased)"
7 .SH NAME
8 pthread_getattr_np \- get attributes of created thread
9 .SH LIBRARY
10 POSIX threads library
11 .RI ( libpthread ", " \-lpthread )
12 .SH SYNOPSIS
13 .nf
14 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
15 .B #include <pthread.h>
16 .PP
17 .BI "int pthread_getattr_np(pthread_t " thread ", pthread_attr_t *" attr );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR pthread_getattr_np ()
22 function initializes the thread attributes object referred to by
23 .I attr
24 so that it contains actual attribute values describing the running thread
25 .IR thread .
26 .PP
27 The returned attribute values may differ from
28 the corresponding attribute values passed in the
29 .I attr
30 object that was used to create the thread using
31 .BR pthread_create (3).
32 In particular, the following attributes may differ:
33 .IP \(bu 3
34 the detach state, since a joinable thread may have detached itself
35 after creation;
36 .IP \(bu
37 the stack size,
38 which the implementation may align to a suitable boundary.
39 .IP \(bu
40 and the guard size,
41 which the implementation may round upward to a multiple of the page size,
42 or ignore (i.e., treat as 0),
43 if the application is allocating its own stack.
44 .PP
45 Furthermore, if the stack address attribute was not set
46 in the thread attributes object used to create the thread,
47 then the returned thread attributes object will report the actual
48 stack address that the implementation selected for the thread.
49 .PP
50 When the thread attributes object returned by
51 .BR pthread_getattr_np ()
52 is no longer required, it should be destroyed using
53 .BR pthread_attr_destroy (3).
54 .SH RETURN VALUE
55 On success, this function returns 0;
56 on error, it returns a nonzero error number.
57 .SH ERRORS
58 .TP
59 .B ENOMEM
60 .\" Can happen (but unlikely) while trying to allocate memory for cpuset
61 Insufficient memory.
62 .PP
63 In addition, if
64 .I thread
65 refers to the main thread, then
66 .BR pthread_getattr_np ()
67 can fail because of errors from various underlying calls:
68 .BR fopen (3),
69 if
70 .I /proc/self/maps
71 can't be opened;
72 and
73 .BR getrlimit (2),
74 if the
75 .B RLIMIT_STACK
76 resource limit is not supported.
77 .SH VERSIONS
78 This function is available in glibc since version 2.2.3.
79 .SH ATTRIBUTES
80 For an explanation of the terms used in this section, see
81 .BR attributes (7).
82 .ad l
83 .nh
84 .TS
85 allbox;
86 lbx lb lb
87 l l l.
88 Interface Attribute Value
89 T{
90 .BR pthread_getattr_np ()
91 T} Thread safety MT-Safe
92 .TE
93 .hy
94 .ad
95 .sp 1
96 .SH STANDARDS
97 This function is a nonstandard GNU extension;
98 hence the suffix "_np" (nonportable) in the name.
99 .SH EXAMPLES
100 The program below demonstrates the use of
101 .BR pthread_getattr_np ().
102 The program creates a thread that then uses
103 .BR pthread_getattr_np ()
104 to retrieve and display its guard size, stack address,
105 and stack size attributes.
106 Command-line arguments can be used to set these attributes
107 to values other than the default when creating the thread.
108 The shell sessions below demonstrate the use of the program.
109 .PP
110 In the first run, on an x86-32 system,
111 a thread is created using default attributes:
112 .PP
113 .in +4n
114 .EX
115 .RB "$" " ulimit \-s" " # No stack limit ==> default stack size is 2 MB"
116 unlimited
117 .RB "$" " ./a.out"
118 Attributes of created thread:
119 Guard size = 4096 bytes
120 Stack address = 0x40196000 (EOS = 0x40397000)
121 Stack size = 0x201000 (2101248) bytes
122 .EE
123 .in
124 .PP
125 In the following run, we see that if a guard size is specified,
126 it is rounded up to the next multiple of the system page size
127 (4096 bytes on x86-32):
128 .PP
129 .in +4n
130 .EX
131 .RB "$" " ./a.out \-g 4097"
132 Thread attributes object after initializations:
133 Guard size = 4097 bytes
134 Stack address = (nil)
135 Stack size = 0x0 (0) bytes
136
137 Attributes of created thread:
138 Guard size = 8192 bytes
139 Stack address = 0x40196000 (EOS = 0x40397000)
140 Stack size = 0x201000 (2101248) bytes
141 .EE
142 .in
143 .\".in +4n
144 .\".nf
145 .\"$ ./a.out \-s 0x8000
146 .\"Thread attributes object after initializations:
147 .\" Guard size = 4096 bytes
148 .\" Stack address = 0xffff8000 (EOS = (nil))
149 .\" Stack size = 0x8000 (32768) bytes
150 .\"
151 .\"Attributes of created thread:
152 .\" Guard size = 4096 bytes
153 .\" Stack address = 0x4001e000 (EOS = 0x40026000)
154 .\" Stack size = 0x8000 (32768) bytes
155 .\".fi
156 .\".in
157 .PP
158 In the last run, the program manually allocates a stack for the thread.
159 In this case, the guard size attribute is ignored.
160 .PP
161 .in +4n
162 .EX
163 .RB "$" " ./a.out \-g 4096 \-s 0x8000 \-a"
164 Allocated thread stack at 0x804d000
165
166 Thread attributes object after initializations:
167 Guard size = 4096 bytes
168 Stack address = 0x804d000 (EOS = 0x8055000)
169 Stack size = 0x8000 (32768) bytes
170
171 Attributes of created thread:
172 Guard size = 0 bytes
173 Stack address = 0x804d000 (EOS = 0x8055000)
174 Stack size = 0x8000 (32768) bytes
175 .EE
176 .in
177 .SS Program source
178 \&
179 .\" SRC BEGIN (pthread_getattr_np.c)
180 .EX
181 #define _GNU_SOURCE /* To get pthread_getattr_np() declaration */
182 #include <err.h>
183 #include <errno.h>
184 #include <pthread.h>
185 #include <stdio.h>
186 #include <stdlib.h>
187 #include <unistd.h>
188
189 static void
190 display_stack_related_attributes(pthread_attr_t *attr, char *prefix)
191 {
192 int s;
193 size_t stack_size, guard_size;
194 void *stack_addr;
195
196 s = pthread_attr_getguardsize(attr, &guard_size);
197 if (s != 0)
198 errc(EXIT_FAILURE, s, "pthread_attr_getguardsize");
199 printf("%sGuard size = %zu bytes\en", prefix, guard_size);
200
201 s = pthread_attr_getstack(attr, &stack_addr, &stack_size);
202 if (s != 0)
203 errc(EXIT_FAILURE, s, "pthread_attr_getstack");
204 printf("%sStack address = %p", prefix, stack_addr);
205 if (stack_size > 0)
206 printf(" (EOS = %p)", (char *) stack_addr + stack_size);
207 printf("\en");
208 printf("%sStack size = %#zx (%zu) bytes\en",
209 prefix, stack_size, stack_size);
210 }
211
212 static void
213 display_thread_attributes(pthread_t thread, char *prefix)
214 {
215 int s;
216 pthread_attr_t attr;
217
218 s = pthread_getattr_np(thread, &attr);
219 if (s != 0)
220 errc(EXIT_FAILURE, s, "pthread_getattr_np");
221
222 display_stack_related_attributes(&attr, prefix);
223
224 s = pthread_attr_destroy(&attr);
225 if (s != 0)
226 errc(EXIT_FAILURE, s, "pthread_attr_destroy");
227 }
228
229 static void * /* Start function for thread we create */
230 thread_start(void *arg)
231 {
232 printf("Attributes of created thread:\en");
233 display_thread_attributes(pthread_self(), "\et");
234
235 exit(EXIT_SUCCESS); /* Terminate all threads */
236 }
237
238 static void
239 usage(char *pname, char *msg)
240 {
241 if (msg != NULL)
242 fputs(msg, stderr);
243 fprintf(stderr, "Usage: %s [\-s stack\-size [\-a]]"
244 " [\-g guard\-size]\en", pname);
245 fprintf(stderr, "\et\et\-a means program should allocate stack\en");
246 exit(EXIT_FAILURE);
247 }
248
249 static pthread_attr_t * /* Get thread attributes from command line */
250 get_thread_attributes_from_cl(int argc, char *argv[],
251 pthread_attr_t *attrp)
252 {
253 int s, opt, allocate_stack;
254 size_t stack_size, guard_size;
255 void *stack_addr;
256 pthread_attr_t *ret_attrp = NULL; /* Set to attrp if we initialize
257 a thread attributes object */
258 allocate_stack = 0;
259 stack_size = \-1;
260 guard_size = \-1;
261
262 while ((opt = getopt(argc, argv, "ag:s:")) != \-1) {
263 switch (opt) {
264 case \(aqa\(aq: allocate_stack = 1; break;
265 case \(aqg\(aq: guard_size = strtoul(optarg, NULL, 0); break;
266 case \(aqs\(aq: stack_size = strtoul(optarg, NULL, 0); break;
267 default: usage(argv[0], NULL);
268 }
269 }
270
271 if (allocate_stack && stack_size == \-1)
272 usage(argv[0], "Specifying \-a without \-s makes no sense\en");
273
274 if (argc > optind)
275 usage(argv[0], "Extraneous command\-line arguments\en");
276
277 if (stack_size >= 0 || guard_size > 0) {
278 ret_attrp = attrp;
279
280 s = pthread_attr_init(attrp);
281 if (s != 0)
282 errc(EXIT_FAILURE, s, "pthread_attr_init");
283 }
284
285 if (stack_size >= 0) {
286 if (!allocate_stack) {
287 s = pthread_attr_setstacksize(attrp, stack_size);
288 if (s != 0)
289 errc(EXIT_FAILURE, s, "pthread_attr_setstacksize");
290 } else {
291 s = posix_memalign(&stack_addr, sysconf(_SC_PAGESIZE),
292 stack_size);
293 if (s != 0)
294 errc(EXIT_FAILURE, s, "posix_memalign");
295 printf("Allocated thread stack at %p\en\en", stack_addr);
296
297 s = pthread_attr_setstack(attrp, stack_addr, stack_size);
298 if (s != 0)
299 errc(EXIT_FAILURE, s, "pthread_attr_setstacksize");
300 }
301 }
302
303 if (guard_size >= 0) {
304 s = pthread_attr_setguardsize(attrp, guard_size);
305 if (s != 0)
306 errc(EXIT_FAILURE, s, "pthread_attr_setstacksize");
307 }
308
309 return ret_attrp;
310 }
311
312 int
313 main(int argc, char *argv[])
314 {
315 int s;
316 pthread_t thr;
317 pthread_attr_t attr;
318 pthread_attr_t *attrp = NULL; /* Set to &attr if we initialize
319 a thread attributes object */
320
321 attrp = get_thread_attributes_from_cl(argc, argv, &attr);
322
323 if (attrp != NULL) {
324 printf("Thread attributes object after initializations:\en");
325 display_stack_related_attributes(attrp, "\et");
326 printf("\en");
327 }
328
329 s = pthread_create(&thr, attrp, &thread_start, NULL);
330 if (s != 0)
331 errc(EXIT_FAILURE, s, "pthread_create");
332
333 if (attrp != NULL) {
334 s = pthread_attr_destroy(attrp);
335 if (s != 0)
336 errc(EXIT_FAILURE, s, "pthread_attr_destroy");
337 }
338
339 pause(); /* Terminates when other thread calls exit() */
340 }
341 .EE
342 .\" SRC END
343 .SH SEE ALSO
344 .ad l
345 .nh
346 .BR pthread_attr_getaffinity_np (3),
347 .BR pthread_attr_getdetachstate (3),
348 .BR pthread_attr_getguardsize (3),
349 .BR pthread_attr_getinheritsched (3),
350 .BR pthread_attr_getschedparam (3),
351 .BR pthread_attr_getschedpolicy (3),
352 .BR pthread_attr_getscope (3),
353 .BR pthread_attr_getstack (3),
354 .BR pthread_attr_getstackaddr (3),
355 .BR pthread_attr_getstacksize (3),
356 .BR pthread_attr_init (3),
357 .BR pthread_create (3),
358 .BR pthreads (7)