1PTHREAD_ATTR_SETGUARDSIZE(3)Linux Programmer's ManuaPlTHREAD_ATTR_SETGUARDSIZE(3)
2
3
4
6 pthread_attr_setguardsize, pthread_attr_getguardsize - set/get guard
7 size attribute in thread attributes object
8
10 #include <pthread.h>
11
12 int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
13 int pthread_attr_getguardsize(pthread_attr_t *attr, size_t *guardsize);
14
15 Compile and link with -pthread.
16
18 The pthread_attr_setguardsize() function sets the guard size attribute
19 of the thread attributes object referred to by attr to the value speci‐
20 fied in guardsize.
21
22 If guardsize is greater than 0, then for each new thread created using
23 attr the system allocates an additional region of at least guardsize
24 bytes at the end of the thread's stack to act as the guard area for the
25 stack (but see BUGS).
26
27 If guardsize is 0, then new threads created with attr will not have a
28 guard area.
29
30 The default guard size is the same as the system page size.
31
32 If the stack address attribute has been set in attr (using
33 pthread_attr_setstack(3) or pthread_attr_setstackaddr(3)), meaning that
34 the caller is allocating the thread's stack, then the guard size
35 attribute is ignored (i.e., no guard area is created by the system): it
36 is the application's responsibility to handle stack overflow (perhaps
37 by using mprotect(2) to manually define a guard area at the end of the
38 stack that it has allocated).
39
40 The pthread_attr_getguardsize() function returns the guard size
41 attribute of the thread attributes object referred to by attr in the
42 buffer pointed to by guardsize.
43
45 On success, these functions return 0; on error, they return a non-zero
46 error number.
47
49 POSIX.1-2001 documents an EINVAL error if attr or guardsize is invalid.
50 On Linux these functions always succeed (but portable and future-proof
51 applications should nevertheless handle a possible error return).
52
54 These functions are provided by glibc since version 2.1.
55
57 POSIX.1-2001.
58
60 A guard area consists of virtual memory pages that are protected to
61 prevent read and write access. If a thread overflows its stack into
62 the guard area, then, on most hard architectures, it receives a SIGSEGV
63 signal, thus notifying it of the overflow. Guard areas start on page
64 boundaries, and the guard size is internally rounded up to the system
65 page size when creating a thread. (Nevertheless, pthread_attr_get‐
66 guardsize() returns the guard size that was set by pthread_attr_set‐
67 guardsize().)
68
69 Setting a guard size of 0 may be useful to save memory in an applica‐
70 tion that creates many threads and knows that stack overflow can never
71 occur.
72
73 Choosing a guard size larger than the default size may be necessary for
74 detecting stack overflows if a thread allocates large data structures
75 on the stack.
76
77 As at glibc 2.8, the NPTL threading implementation includes the guard
78 area within the stack size allocation, rather than allocating extra
79 space at the end of the stack, as POSIX.1 requires. (This can result
80 in an EINVAL error from pthread_create(3) if the guard size value is
81 too large, leaving no space for the actual stack.)
82
83 The obsolete LinuxThreads implementation did the right thing, allocat‐
84 ing extra space at the end of the stack for the guard area.
85
87 See pthread_getattr_np(3).
88
90 mmap(2), mprotect(2), pthread_attr_init(3), pthread_create(3),
91 pthread_attr_setstack(3), pthread_attr_setstacksize(3), pthreads(7)
92
94 This page is part of release 3.22 of the Linux man-pages project. A
95 description of the project, and information about reporting bugs, can
96 be found at http://www.kernel.org/doc/man-pages/.
97
98
99
100Linux 2008-10-24 PTHREAD_ATTR_SETGUARDSIZE(3)