1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH pthread_attr_setguardsize 3 (date) "Linux man-pages (unreleased)"
8 pthread_attr_setguardsize, pthread_attr_getguardsize \- set/get guard size
9 attribute in thread attributes object
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
17 .BI "int pthread_attr_setguardsize(pthread_attr_t *" attr \
18 ", size_t " guardsize );
19 .BI "int pthread_attr_getguardsize(const pthread_attr_t *restrict " attr ,
20 .BI " size_t *restrict " guardsize );
24 .BR pthread_attr_setguardsize ()
25 function sets the guard size attribute of the
26 thread attributes object referred to by
28 to the value specified in
34 then for each new thread created using
36 the system allocates an additional region of at least
38 bytes at the end of the thread's stack to act as the guard area
39 for the stack (but see BUGS).
43 is 0, then new threads created with
45 will not have a guard area.
47 The default guard size is the same as the system page size.
49 If the stack address attribute has been set in
52 .BR pthread_attr_setstack (3)
54 .BR pthread_attr_setstackaddr (3)),
55 meaning that the caller is allocating the thread's stack,
56 then the guard size attribute is ignored
57 (i.e., no guard area is created by the system):
58 it is the application's responsibility to handle stack overflow
61 to manually define a guard area at the end of the stack
62 that it has allocated).
65 .BR pthread_attr_getguardsize ()
66 function returns the guard size attribute of the
67 thread attributes object referred to by
69 in the buffer pointed to by
72 On success, these functions return 0;
73 on error, they return a nonzero error number.
82 On Linux these functions always succeed
83 (but portable and future-proof applications should nevertheless
84 handle a possible error return).
86 These functions are provided by glibc since version 2.1.
88 For an explanation of the terms used in this section, see
96 Interface Attribute Value
98 .BR pthread_attr_setguardsize (),
99 .BR pthread_attr_getguardsize ()
100 T} Thread safety MT-Safe
106 POSIX.1-2001, POSIX.1-2008.
108 A guard area consists of virtual memory pages that are protected
109 to prevent read and write access.
110 If a thread overflows its stack into the guard area,
111 then, on most hard architectures, it receives a
113 signal, thus notifying it of the overflow.
114 Guard areas start on page boundaries,
115 and the guard size is internally rounded up to
116 the system page size when creating a thread.
118 .BR pthread_attr_getguardsize ()
119 returns the guard size that was set by
120 .BR pthread_attr_setguardsize ().)
122 Setting a guard size of 0 may be useful to save memory
123 in an application that creates many threads
124 and knows that stack overflow can never occur.
126 Choosing a guard size larger than the default size
127 may be necessary for detecting stack overflows
128 if a thread allocates large data structures on the stack.
130 As at glibc 2.8, the NPTL threading implementation includes
131 the guard area within the stack size allocation,
132 rather than allocating extra space at the end of the stack,
134 (This can result in an
137 .BR pthread_create (3)
138 if the guard size value is too large,
139 leaving no space for the actual stack.)
141 The obsolete LinuxThreads implementation did the right thing,
142 allocating extra space at the end of the stack for the guard area.
143 .\" glibc includes the guardsize within the allocated stack size,
144 .\" which looks pretty clearly to be in violation of POSIX.
146 .\" Filed bug, 22 Oct 2008:
147 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=6973
150 .\" https//bugzilla.redhat.com/show_bug.cgi?id=435337
151 .\" Reportedly, LinuxThreads did the right thing, allocating
152 .\" extra space at the end of the stack:
153 .\" http://sourceware.org/ml/libc-alpha/2008-05/msg00086.html
156 .BR pthread_getattr_np (3).
160 .BR pthread_attr_init (3),
161 .BR pthread_attr_setstack (3),
162 .BR pthread_attr_setstacksize (3),
163 .BR pthread_create (3),