Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean "main thread can dynamically grow its stack"

On man page for pthread_attr_setstacksize()

A thread's stack size is fixed at the time of thread creation. Only the main thread can dynamically grow its stack.

My understanding on linux pthread, the main thread stack size is limited to ulimit -s value on main thread creating. Although it maps phy to virt on demand of stack usage, the size is not grown any more.

What does the dynamically grow mean here? Does it imply main thread stack size can grow exceeding ulimit -s?

like image 627
Leslie Li Avatar asked Sep 03 '26 19:09

Leslie Li


1 Answers

The value set by ulimit -s (aka setrlimit(RLIMIT_STACK, ...)), usually 8 MB by default, is the maximum stack size. Initially, a much smaller amount of virtual memory will be allocated and mapped (perhaps just a few kb). When the stack grows larger than the amount actually allocated, it triggers a page fault. The kernel then compares the current usage with the maximum value set in the rlimit. If the maximum has not been reached, the kernel allocates more pages of virtual memory and maps them into place, then returns control to the process; this is completely transparent. If the maximum is reached, it kills the process with SIGSEGV.

It would be inefficient if the system had to reserve a full 8 MB of virtual memory for every process, when most will use far less. By allocating it only as needed, you can still have hundreds of processes, each with an 8 MB stack limit, even if the machine has only (let's say) 64 MB of memory + swap total. It's a form of overcommitment.

Also keep in mind that a process can call setrlimit itself at run time and increase its own maximum stack size, so long as nothing else has been mapped into that address space. The main thread's stack is traditionally located near the top of virtual memory, with everything else near the bottom, so that there is a lot of free address space in between, and so increasing the maximum beyond its initial 8 MB limit is usually possible. However, the stacks of other threads necessarily must be allocated elsewhere, and it is not really possible to ensure that there is a lot of free address space for them to grow into.

like image 63
Nate Eldredge Avatar answered Sep 06 '26 08:09

Nate Eldredge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!