1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREAD_ATTR_SETGUARDSIZE 3 2008-10-24 "Linux" "Linux Programmer's Manual"
26 pthread_attr_setguardsize, pthread_attr_getguardsize \- set/get guard size
27 attribute in thread attributes object
30 .B #include <pthread.h>
32 .BI "int pthread_attr_setguardsize(pthread_attr_t *" attr \
33 ", size_t " guardsize );
34 .BI "int pthread_attr_getguardsize(pthread_attr_t *" attr \
35 ", size_t *" guardsize );
37 Compile and link with \fI\-pthread\fP.
40 .BR pthread_attr_setguardsize ()
41 function sets the guard size attribute of the
42 thread attributes object referred to by
44 to the value specified in
50 then for each new thread created using
52 the system allocates an additional region of at least
54 bytes at the end of the thread's stack to act as the guard area
55 for the stack (but see BUGS).
59 is 0, then new threads created with
61 will not have a guard area.
63 The default guard size is the same as the system page size.
65 If the stack address attribute has been set in
68 .BR pthread_attr_setstack (3)
70 .BR pthread_attr_setstackaddr (3)),
71 meaning that the caller is allocating the thread's stack,
72 then the guard size attribute is ignored
73 (i.e., no guard area is created by the system):
74 it is the application's responsibility to handle stack overflow
77 to manually define a guard area at the end of the stack
78 that it has allocated).
81 .BR pthread_attr_getguardsize ()
82 function returns the guard size attribute of the
83 thread attributes object referred to by
85 in the buffer pointed to by
88 On success, these functions return 0;
89 on error, they return a nonzero error number.
91 POSIX.1-2001 documents an
98 On Linux these functions always succeed
99 (but portable and future-proof applications should nevertheless
100 handle a possible error return).
102 These functions are provided by glibc since version 2.1.
106 A guard area consists of virtual memory pages that are protected
107 to prevent read and write access.
108 If a thread overflows its stack into the guard area,
109 then, on most hard architectures, it receives a
111 signal, thus notifying it of the overflow.
112 Guard areas start on page boundaries,
113 and the guard size is internally rounded up to
114 the system page size when creating a thread.
116 .BR pthread_attr_getguardsize ()
117 returns the guard size that was set by
118 .BR pthread_attr_setguardsize ().)
120 Setting a guard size of 0 may be useful to save memory
121 in an application that creates many threads
122 and knows that stack overflow can never occur.
124 Choosing a guard size larger than the default size
125 may be necessary for detecting stack overflows
126 if a thread allocates large data structures on the stack.
128 As at glibc 2.8, the NPTL threading implementation includes
129 the guard area within the stack size allocation,
130 rather than allocating extra space at the end of the stack,
132 (This can result in an
135 .BR pthread_create (3)
136 if the guard size value is too large,
137 leaving no space for the actual stack.)
139 The obsolete LinuxThreads implementation did the right thing,
140 allocating extra space at the end of the stack for the guard area.
141 .\" glibc includes the guardsize within the allocated stack size,
142 .\" which looks pretty clearly to be in violation of POSIX.
144 .\" Filed bug, 22 Oct 2008:
145 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=6973
148 .\" https//bugzilla.redhat.com/show_bug.cgi?id=435337
149 .\" Reportedly, LinuxThreads did the right thing, allocating
150 .\" extra space at the end of the stack:
151 .\" http://sourceware.org/ml/libc-alpha/2008-05/msg00086.html
154 .BR pthread_getattr_np (3).
158 .BR pthread_attr_init (3),
159 .BR pthread_attr_setstack (3),
160 .BR pthread_attr_setstacksize (3),
161 .BR pthread_create (3),