2

So i was reading this question :

https://stackoverflow.com/questions/1762418/what-resources-are-shared-between-threads

and based on the answer, "threads share all segments except the stack, but a thread can still access the stack of another thread"??

but i still have three questions :

  1. do all threads of a process work in the same virtual memory space?

  2. when we say threads can still access stack of other threads, does this mean for example if our stack starts from 0x00 to 0xff then one thread might work from 0x00 to 0x0f and the other one works from 0x10 to 0xff? or ..?

  3. is stack the only part in virtual memory that is unique per thread in linux?

1 Answer 1

4
  1. Do all threads of a process work in the same virtual address space?

Yes. Each thread has its own stack, but all the memory associated with the process is in the same virtual address space. If a function in one thread had a pointer to a stack-allocated variable in another thread, that function could read from/write to that variable.

  1. When we say threads can still access stack of other threads, dos that mean for example if our stack starts from 0x00 to 0xff then one thread might work from 0x00 to 0x0f and the other one works for 0x10 to 0xff?

Like I said in my response to your first question, each thread gets its own stack. In the abstract, that means that one thread's stack runs from A to B, and a different thread's stack runs from C to D, where A < B < C < D (i.e., the A-B range doesn't overlap with the C-D range).

  1. Is the stack the only part in virtual memory that is unique per thread in Linux?

The uniqueness of the stacks isn't related to virtual memory. The stack (really "activation record stack") is used to store information about running functions (both the currently running function, and all of the calling functions). If there are multiple threads, then there are multiple functions (and calling functions). If the threads were to share a stack where the activation records were interleaved, it's be almost impossible to know to which calling function to return when a function finishes.

3
  • So basically the stack part in the virtual memory is "cut up" into multiple parts, each of them belonging to a thread and they can still be accessed by all threads, correct?
    – John P
    Oct 25, 2018 at 14:37
  • A stack is just a chunk of memory. Functions have two options for storing data: CPU registers and the stack. Each activation (call) to a function pushes an "activation record" on the stack. The activation record includes locals that won't fit in registers as well as the old values of registers that need to be restored before the function returns. All threads can access addresses from all thread's stacks because they're all in the same virtual address space. Oct 25, 2018 at 17:39
  • 1
    Which means there are more than two growable segments in the process' virtual address space (stacks for each thread, the conventional heap, mmaps) so it is possible to have a collision long before the address space is fully exhaused.
    – benjimin
    Mar 5, 2020 at 6:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .