[Operating system] how to thread stack allocation

Test environment:
Linux CentOS-7.shared 3.10.0-693.5.2.el7.x86_64 # 1 SMP Fri Oct 20 20:32:50 UTC 2017 x86_64 x86_64 x86_64 the GNU / Linux

A process virtual address space can be roughly divided into a general code area (text), a read only data area (rodata), initialization data area (data), the initialization data area (BSS), the heap (heap), the shared memory area (. so, mmap place), the stack (stack), the core area (kernel).

Here Insert Picture Description

For Linux process or the main thread, its stack is generated when the fork is actually copied his father's stack address space, and then copy (cow) and dynamic growth when writing.
However, for the main thread to create a sub-thread, its stack will no longer be such, but rather fixed in advance.
Thread stack can not grow dynamically, once exhausted is gone, which is the process of generating and fork in different places.

Thread (not the main thread) is of fixed size stackWhich will (near the top of the heap allocated top-down) stack idle or idle stack (distribution near the bottom of the stack upwardly from the bottom), so the thread stack variables allocated local function is located in the respectively assigned stack space, thus can be said thread private, but also because the thread stack is to set good boundaries, so the thread stack of fixed size.

test

ulimit -aCheck your operating system's restrictions:
you can see the limit stack size is 8192kb is 8MB.
Note here 8MB means that each thread is created stack is so big.

[parallels@centos-7 LinuxCode]$ ulimit -a
......
stack size              (kbytes, -s) 8192
......

Test code:
create a three thread, execution ThreadEntry.
Compiled to run up!

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

void*
ThreadEntry(void* args)
{
    (void) args;
    while (1)
    {
        sleep(1);
    }
}

int main()
{
    pthread_t tid1, tid2, tid3;
    pthread_create(&tid1, NULL, ThreadEntry, NULL);
    pthread_create(&tid2, NULL, ThreadEntry, NULL);
    pthread_create(&tid3, NULL, ThreadEntry, NULL);
    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);
    pthread_join(tid3, NULL);
    return 0;
}

Downwardly from the top of the heap allocation.
ps aux | grep a.outView pid
cat /proc/[pid]/mapsThis process maps show the area of memory and access rights.
It can be seen: in the following several consecutive heap property rw-paddress are just the right size 8192kb. And each interspersed at the border of a size 1000H (4096kb) spatial boundary.

Here Insert Picture Description

Dispensing from the bottom stack
ulimit -s unlimitedSet stack size is unlimited, note that although the set stack size is unlimited, but in fact it is not infinite, but also a fixed thread stack size, the size of 1mb.
Then cat /proc/[pid]/mapsview map virtual address space.
It can be seen that, in this case thread stack is allocated in the vicinity of the bottom stack, the bottom-up growth.
Here Insert Picture Description
Here Insert Picture Description

in conclusion

Whether thread stack is allocated on the stack or heap allocation, which are fixed size, there are boundaries.


Reference:
https://blog.csdn.net/qq_16097611/article/details/82592873
https://blog.csdn.net/yangkuanqaz85988/article/details/52403726
https://blog.csdn.net/lijzheng/article/ details / 23618365

EOF

Published 73 original articles · won praise 90 · views 40000 +

Guess you like

Origin blog.csdn.net/Hanoi_ahoj/article/details/105115117