Memory Layout Theme

I understand how the process memory format looks like (Code, Data, Heap, Stack).

I do not understand, however, what a memory layout looks like in a program with multiple threads.

In the end, there is one stack for a process, so I would suggest that all threads somehow use the same stack. But this does not seem right, because each thread has its own stack, and threads are not guaranteed to be executed in the order in which they were called, so putting them one by one on the process stack does not make much sense to me.

I came across this picture on the net:

Thread memory layout

It is shown here that each thread has its own stack, which makes sense, as well as its own kernel stack.

Does this mean that I (using the figure) have 3 "processes"? (Let's say that the address space of the process is 4 GB, then 3 threads will be 12 GB? I think not ..)

I want to understand where each of the thread stack is in memory.

I know that all threads share data and code segments, so I would assume that either the heap will contain stream stacks or they will sit in kernel space.

I would really like to know what will happen.

Many ratings.

+5
source share

Source: https://habr.com/ru/post/1237686/


All Articles