1pthread_attr_setguardsize(3)Library Functions Manualpthread_attr_setguardsize(3)
2
3
4

NAME

6       pthread_attr_setguardsize,  pthread_attr_getguardsize  -  set/get guard
7       size attribute in thread attributes object
8

LIBRARY

10       POSIX threads library (libpthread, -lpthread)
11

SYNOPSIS

13       #include <pthread.h>
14
15       int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
16       int pthread_attr_getguardsize(const pthread_attr_t *restrict attr,
17                                     size_t *restrict guardsize);
18

DESCRIPTION

20       The pthread_attr_setguardsize() function sets the guard size  attribute
21       of the thread attributes object referred to by attr to the value speci‐
22       fied in guardsize.
23
24       If guardsize is greater than 0, then for each new thread created  using
25       attr  the  system  allocates an additional region of at least guardsize
26       bytes at the end of the thread's stack to act as the guard area for the
27       stack (but see BUGS).
28
29       If  guardsize  is 0, then new threads created with attr will not have a
30       guard area.
31
32       The default guard size is the same as the system page size.
33
34       If  the  stack  address  attribute  has  been  set   in   attr   (using
35       pthread_attr_setstack(3) or pthread_attr_setstackaddr(3)), meaning that
36       the caller is allocating the thread's stack, then the guard size attri‐
37       bute  is  ignored (i.e., no guard area is created by the system): it is
38       the application's responsibility to handle stack overflow  (perhaps  by
39       using  mprotect(2)  to  manually  define a guard area at the end of the
40       stack that it has allocated).
41
42       The pthread_attr_getguardsize() function returns the guard size  attri‐
43       bute  of the thread attributes object referred to by attr in the buffer
44       pointed to by guardsize.
45

RETURN VALUE

47       On success, these functions return 0; on error, they return  a  nonzero
48       error number.
49

ERRORS

51       POSIX.1  documents an EINVAL error if attr or guardsize is invalid.  On
52       Linux these functions always succeed (but portable and future-proof ap‐
53       plications should nevertheless handle a possible error return).
54

ATTRIBUTES

56       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
57       tributes(7).
58
59       ┌────────────────────────────────────────────┬───────────────┬─────────┐
60Interface                                   Attribute     Value   
61       ├────────────────────────────────────────────┼───────────────┼─────────┤
62pthread_attr_setguardsize(),                │ Thread safety │ MT-Safe │
63pthread_attr_getguardsize()                 │               │         │
64       └────────────────────────────────────────────┴───────────────┴─────────┘
65

STANDARDS

67       POSIX.1-2008.
68

HISTORY

70       glibc 2.1.  POSIX.1-2001.
71

NOTES

73       A  guard  area  consists  of virtual memory pages that are protected to
74       prevent read and write access.  If a thread overflows  its  stack  into
75       the guard area, then, on most hard architectures, it receives a SIGSEGV
76       signal, thus notifying it of the overflow.  Guard areas start  on  page
77       boundaries,  and  the guard size is internally rounded up to the system
78       page    size    when     creating     a     thread.      (Nevertheless,
79       pthread_attr_getguardsize()  returns  the  guard  size  that was set by
80       pthread_attr_setguardsize().)
81
82       Setting a guard  size  of  0  may  be  useful  to  save  memory  in  an
83       application that creates many threads and knows that stack overflow can
84       never occur.
85
86       Choosing a guard size larger than the default size may be necessary for
87       detecting  stack  overflows if a thread allocates large data structures
88       on the stack.
89

BUGS

91       As at glibc 2.8, the NPTL threading implementation includes  the  guard
92       area  within  the  stack  size allocation, rather than allocating extra
93       space at the end of the stack, as POSIX.1 requires.  (This  can  result
94       in  an  EINVAL  error from pthread_create(3) if the guard size value is
95       too large, leaving no space for the actual stack.)
96
97       The  obsolete  LinuxThreads  implementation  did   the   right   thing,
98       allocating extra space at the end of the stack for the guard area.
99

EXAMPLES

101       See pthread_getattr_np(3).
102

SEE ALSO

104       mmap(2),  mprotect(2),  pthread_attr_init(3), pthread_attr_setstack(3),
105       pthread_attr_setstacksize(3), pthread_create(3), pthreads(7)
106
107
108
109Linux man-pages 6.05              2023-07-20      pthread_attr_setguardsize(3)
Impressum