Reputation: 14970
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?
Upvotes: 3
Views: 896
Reputation: 45025
Yes, under the POSIX threading model, each thread has its own stack.
Upvotes: 1
Reputation: 1707
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.
Upvotes: 2