3

I have a simple question about Linux threads and processes.

The process in Linux has a separate virtual address space which consists of:

- stack
- heap
- bss
- code/text
- data

A process can have multiple threads inside them. I understand that they do share the address space of the process.

However, since the function calls that are executed by different threads can be different, does a thread have a separate stack segment?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
liv2hak
  • 14,472
  • 53
  • 157
  • 270

2 Answers2

2
  1. Threads share all the memory segments, including the stack segment.
  2. Each thread has a separate stack.

Both statements are true. I know they sound like a contradiction.

The first thread's stack uses the so-called "stack segment". It's allocated by the kernel.

# cat /proc/self/maps
...
7fffbe0b0000-7fffbe0d1000 rw-p 00000000 00:00 0                          [stack]
...

Threads created later (e.g. created by pthread_create() or clone(CLONE_VM)) use the heap (or private anonymous mmap which is the same as heap in every ways) as their stack. It's allocated by the user program and passed to clone().

In short, each thread uses a separate stack. All the threads can read/write every other threads' stack.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Wu Yongzheng
  • 1,707
  • 17
  • 23
  • 1
    In which case the "stack segment" register probably doesn't point to the process's stack segment (in any but the original thread). – Ben Voigt Aug 27 '14 at 01:56
  • @BenVoigt: The "stack segment" register is a segmented-memory concept from old x86 and is not applicable to Linux or any modern OS. On x86 Linux, the stack segment register is set to the same value as the data segment register, and both refer to a protected mode selector for the full 4GB linear memory range with zero offset. The "stack segment" has no relation to what portion of this linear memory is actually being used for the stack. – R.. GitHub STOP HELPING ICE Aug 27 '14 at 02:09
  • @BenVoigt exactly. Thread X's stack pointer (e.g. esp) points to memory region Y is what I meant by thread X USES Y as its stack. – Wu Yongzheng Aug 27 '14 at 02:13
  • @R.., I think what BenVoigt meant is stack pointer, not stack segment. Nobody care about segment registers now. – Wu Yongzheng Aug 27 '14 at 02:17
  • @WuYongzheng - exactly the answer that I was looking for. – liv2hak Aug 27 '14 at 02:19
  • @WuYongzheng: In that case, the comment is wrong. Each thread has its own full set of register values, including a stack pointer. Or maybe I just misread what he was saying...? It's not clear. – R.. GitHub STOP HELPING ICE Aug 27 '14 at 03:28
  • @WuYongzheng - there is no such thing called a "private anonymous mmap).It can either be "private" or it can be "anonymous" – liv2hak Sep 04 '14 at 07:41
  • @liv2hak Yes, there is. RTFM. Another way to learn is strace. Try `strace echo 1 2>&1 | grep MAP_PRIVATE.*MAP_ANONYMOUS` – Wu Yongzheng Sep 04 '14 at 15:16
1

Yes, under the POSIX threading model, each thread has its own stack.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
  • does this mean that there is a separate area in the stack segment reserved for each thread in the process? AFAIK the switching between the threads is done by the process itself without interference from the OS scheduler? – liv2hak Aug 26 '14 at 23:21
  • 2
    @liv2hak wot? Thread management IS the OS kernel scheduler/dispatcher responsibility. – Martin James Aug 26 '14 at 23:58
  • @Jim Lewis - see http://stackoverflow.com/questions/9651871/whats-the-difference-between-the-threadsand-process-in-kernel-mode-and-ones-i – liv2hak Aug 27 '14 at 00:00
  • @liv2hak: The answer to the question you linked explains that Linux does not use userspace thread scheduling (and in fact, it's basically impossible to implement POSIX threads that way due to signal and blocking syscall semantics). – R.. GitHub STOP HELPING ICE Aug 27 '14 at 02:07