Книга: Programming with POSIX® Threads

10.1.4 Stack guard size

10.1.4 Stack guard size

Guard size comes from DCE threads. Most thread implementations add to the thread's stack a "guard" region, a page or more of protected memory. This protected page is a safety zone, to prevent a stack overflow in one thread from corrupting another thread's stack. There are two good reasons for wanting to control a thread's guard size:

1. It allows an application or library that allocates large data arrays on the stack to increase the default guard size. For example, if a thread allocates two pages at once, a single guard page provides little protection against stack overflows — the thread can corrupt adjoining memory without touching the protected page.

2. When creating a large number of threads, it may be that the extra page for each stack can become a severe burden. In addition to the extra page, the kernel's memory manager has to keep track of the differing protection on adjoining pages, which may strain system resources. Therefore, you may sometimes need to ask the system to "trust you" and avoid allocating any guard pages at all for your threads. You can do this by requesting a guard size of 0 bytes.

pthread_attr_getguardsize

int pthread_attr_getguardsize (

const pthread_attr_t *attr, size_t *guardsize);

Determine the size of the guard region for the stack on which threads created with attr will run.

References: 2, 5.2.3

Errors: [EINVAL] attr invalid.

Hint: Specify 0 to fit lots of stacks in an address space, or increase default

guardsize for threads that allocate large buffers on the stack.

pthread_attr_setguardsize

int pthread_attr_setguardsize (

pthread_attr_t *attr,

size_t guardsize);

Threads created with attr will run on a stack with guardsize bytes protected against stack overflow. The implementation may round guardsize up to the next multiple of PAGESIZE. Specifying a value of 0 for guardsize will cause threads created using the attributes object to run without stack overflow protection.

References: 2, 5.2.3

Errors: [EINVAL] guardsize or attr invalid.

Hint: Specify 0 to fit lots of stacks in an address space, or increase default

guardsize for threads that allocate large buffers on the stack.

Оглавление книги


Генерация: 1.165. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз