6
votes

I want to know the full detail of the address space layout of a multithreaded Linux Process for both 64 bit and 32 bit. Link to any article that describes it will be appreciated. And note that I need to know full details, not just an overview, because I will be directly dealing with it. So I need to know for example, where are the thread stacks located, the heap, thread private data etc...

1
Especially I want to know details about portion which are writable. For example I don't need to deal with the part of the address space that contains the code, since its not writable.MetallicPriest
Do you know all details of no-threaded linux process, don't you? The difference between non-threaded and multithreaded is number and place of stacks.osgx

1 Answers

7
votes

Thread stacks are allocated with mmap at thread start (or even before - you can set the stack space in pthread_attrs). TLS data is stored in the beginning of thread's stack. Size of thread's stacks is fixed, typically it is from 2 to 8 MB. Stack size of each thread can't be changed while the thread is live. (First thread - running main - is still uses main stack at the end of address space and this stack may grow and shrink.) Heap and code is shared between all threads. Mutexes can be anywhere in data section - it is just a struct.

The mmap of thread's stack is not fixed at any address:

Glibc sources

 mem = mmap (NULL, size, prot,
                  MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);

PS modern GCC allows threads stack to be unlimited with SplitStacks feature