The stacks for the other threads located in a process virtual address space

assemblyprocess

The following image shows where the sections of a process are laid out in the process's virtual address space:

enter image description here

You can see that there is only one stack section (since this process only has one thread I assume).

But what if this process has another thread, where will the stack for this second thread be located? will it be located immediately below the first stack?

Best Answer

That's a pretty old and mostly obsolete model for virtual memory layout.

In reality instructions and global each start at some separate random location. Linked libraries are mapped separately from the main program to their own random locations.

The heap is created by asking for blank pages as needed. These may or may not be contiguous to previously returned pages of memory.

The first and last chunk of virtual memory is often reserved and marked non-accessible to catch null pointer related bugs.

Even after all that there will be enough free space to reserve space (again in random locations) for the stack of each thread created.

Related Topic