Reputation: 780
I am aware that the threads of a process share the address space (all the segments) except the stack. Each thread has it's own stack. I have also read that being in same address space, threads can have access to the memory location belonging to the stack of other thread (here and here).
If above statements are correct, I've this question: what happens when OS sees such address from a thread? What if threads write something in other's stack and corrupt them? why OS doesn't generate some sort of permission denied error?
If answer for this question depends on operating systems, please consider Linux.
Upvotes: 2
Views: 3400
Reputation: 21607
As you state, threads share the same address space. Thus each thread has the same access to the address space as does any other thread.
OS sees such address from a thread?
I can't tell what you are asking here.
What if threads write something in other's stack and corrupt them?
This is entirely possible and the results are unpredictable. It just like a race condition.
why OS doesn't generate some sort of permission denied error?
Memory access is controlled by processor mode (e.g., user, kernel, and on some systems additional modes). Generally mode level protection is designed to prevent user access to the shared system range of the logical address space. Processes are protected from each other by having different user address space ranges.
Generally, you want threads to be able to read and write within the same address space. Otherwise, you might as well just use separate processes.
The stack is simply a block of read/write member. There is nothing sacred about a stack. Any block of memory can be a stack just be using it as a stack.
Upvotes: 2