Reputation: 1987
I found this link:
one quick question about stack of thread and process
I understand why threads have separate stacks, but I read that the process may have two stacks . Why is this? could it be because we count the process stack + the thread stack?
Upvotes: 2
Views: 2813
Reputation: 228
Modern operating systems allocate something called kernel interrupt stack for every user-level process. By user-level process, I mean any process that is not running in the kernel-mode. This makes it easier to switch to another process after a processor interrupt is handled. Suppose your processor is running process x that requires a system call to the kernel, and then a processor interrupt happened, which makes the processor run the handler for this interrupt. After this handler is done, the operating system can then choose to run process y instead of continuing process x. To support this type of switch, you need this kernel interrupt stack. In which we save the whole execution environment for later use. After process y is done, x can continue doing its system call without any problems. Note that if your user-level process doesn't have any system calls to the kernel-level, then you will have an empty kernel interrupt stack. This may be the second stack that you are talking about. Two Stacks Illustration
This is used many times, not only to handle processor interrupts. Sometimes the process is ready and waiting its turn to continue executing, then we have its state saved in its kernel interrupt stack, and when we start executing we fetch the state and put it in the registers. Also, if the process requests an I/O operation to be done, this process is suspended till the I/O event is done, after that, it continues to execute with the same procedure explained above.
Due to memory limitations, UNIX started with few kernel interrupt stacks for all processes, not kernel interrupt stack for each process. However, after the memory became much cheaper, it's now easier to have kernel stack for each process.
For further information about it, you can review chapter 2 Kernel Abstraction and chapter 4 Concurrency and Threads in "Operating Systems: Principles and Practice 2nd Edition"
Upvotes: 2
Reputation: 15758
It is explained there:
So, if you say, that "One process can have two stacks" is partially true. The process itself does not have any stacks, but its threads have - as many as the number of threads.
Upvotes: 4