OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_attr_setguardsize.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH PTHREAD_ATTR_SETGUARDSIZE 3 2014-05-28 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_attr_setguardsize, pthread_attr_getguardsize \- set/get guard size
29 attribute in thread attributes object
30 .SH SYNOPSIS
31 .nf
32 .B #include <pthread.h>
33
34 .BI "int pthread_attr_setguardsize(pthread_attr_t *" attr \
35 ", size_t " guardsize );
36 .BI "int pthread_attr_getguardsize(const pthread_attr_t *" attr \
37 ", size_t *" guardsize );
38 .sp
39 Compile and link with \fI\-pthread\fP.
40 .fi
41 .SH DESCRIPTION
42 The
43 .BR pthread_attr_setguardsize ()
44 function sets the guard size attribute of the
45 thread attributes object referred to by
46 .I attr
47 to the value specified in
48 .IR guardsize .
49
50 If
51 .I guardsize
52 is greater than 0,
53 then for each new thread created using
54 .I attr
55 the system allocates an additional region of at least
56 .I guardsize
57 bytes at the end of the thread's stack to act as the guard area
58 for the stack (but see BUGS).
59
60 If
61 .I guardsize
62 is 0, then new threads created with
63 .I attr
64 will not have a guard area.
65
66 The default guard size is the same as the system page size.
67
68 If the stack address attribute has been set in
69 .I attr
70 (using
71 .BR pthread_attr_setstack (3)
72 or
73 .BR pthread_attr_setstackaddr (3)),
74 meaning that the caller is allocating the thread's stack,
75 then the guard size attribute is ignored
76 (i.e., no guard area is created by the system):
77 it is the application's responsibility to handle stack overflow
78 (perhaps by using
79 .BR mprotect (2)
80 to manually define a guard area at the end of the stack
81 that it has allocated).
82
83 The
84 .BR pthread_attr_getguardsize ()
85 function returns the guard size attribute of the
86 thread attributes object referred to by
87 .I attr
88 in the buffer pointed to by
89 .IR guardsize .
90 .SH RETURN VALUE
91 On success, these functions return 0;
92 on error, they return a nonzero error number.
93 .SH ERRORS
94 POSIX.1-2001 documents an
95 .B EINVAL
96 error if
97 .I attr
98 or
99 .I guardsize
100 is invalid.
101 On Linux these functions always succeed
102 (but portable and future-proof applications should nevertheless
103 handle a possible error return).
104 .SH VERSIONS
105 These functions are provided by glibc since version 2.1.
106 .SH ATTRIBUTES
107 .SS Multithreading (see pthreads(7))
108 The
109 .BR pthread_attr_setguardsize ()
110 and
111 .BR pthread_attr_getguardsize ()
112 functions are thread-safe.
113 .SH CONFORMING TO
114 POSIX.1-2001.
115 .SH NOTES
116 A guard area consists of virtual memory pages that are protected
117 to prevent read and write access.
118 If a thread overflows its stack into the guard area,
119 then, on most hard architectures, it receives a
120 .B SIGSEGV
121 signal, thus notifying it of the overflow.
122 Guard areas start on page boundaries,
123 and the guard size is internally rounded up to
124 the system page size when creating a thread.
125 (Nevertheless,
126 .BR pthread_attr_getguardsize ()
127 returns the guard size that was set by
128 .BR pthread_attr_setguardsize ().)
129
130 Setting a guard size of 0 may be useful to save memory
131 in an application that creates many threads
132 and knows that stack overflow can never occur.
133
134 Choosing a guard size larger than the default size
135 may be necessary for detecting stack overflows
136 if a thread allocates large data structures on the stack.
137 .SH BUGS
138 As at glibc 2.8, the NPTL threading implementation includes
139 the guard area within the stack size allocation,
140 rather than allocating extra space at the end of the stack,
141 as POSIX.1 requires.
142 (This can result in an
143 .B EINVAL
144 error from
145 .BR pthread_create (3)
146 if the guard size value is too large,
147 leaving no space for the actual stack.)
148
149 The obsolete LinuxThreads implementation did the right thing,
150 allocating extra space at the end of the stack for the guard area.
151 .\" glibc includes the guardsize within the allocated stack size,
152 .\" which looks pretty clearly to be in violation of POSIX.
153 .\"
154 .\" Filed bug, 22 Oct 2008:
155 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=6973
156 .\"
157 .\" Older reports:
158 .\" https//bugzilla.redhat.com/show_bug.cgi?id=435337
159 .\" Reportedly, LinuxThreads did the right thing, allocating
160 .\" extra space at the end of the stack:
161 .\" http://sourceware.org/ml/libc-alpha/2008-05/msg00086.html
162 .SH EXAMPLE
163 See
164 .BR pthread_getattr_np (3).
165 .SH SEE ALSO
166 .BR mmap (2),
167 .BR mprotect (2),
168 .BR pthread_attr_init (3),
169 .BR pthread_attr_setstack (3),
170 .BR pthread_attr_setstacksize (3),
171 .BR pthread_create (3),
172 .BR pthreads (7)
173 .SH COLOPHON
174 This page is part of release 3.79 of the Linux
175 .I man-pages
176 project.
177 A description of the project,
178 information about reporting bugs,
179 and the latest version of this page,
180 can be found at
181 \%http://www.kernel.org/doc/man\-pages/.