开发者

Does every routine called by the OS create it's own 'stack'?

开发者 https://www.devze.com 2023-02-08 23:06 出处:网络
Sorry, simple question but it is something that is always confused me when speaking about Operating Systems theory.

Sorry, simple question but it is something that is always confused me when speaking about Operating Systems theory.

My book claims that reentrancy is made possible by using "the stack" - does this mean that there is one 'stack' and it is, somehow, shared among all routines, or does "the stack" refer to the abstract idea of a开发者_开发技巧 stack, and a stack is created and stored in main memory every time some subroutine is called ? (Assuming it is needed).

And what about the Stack Pointer in the CPU in this case ? How does that function, in either of these cases ?

Apologies for the vagueness of my description, the whole idea is just kind of shrouded in conflicting ideas for me !


This is platform specific (depends on used hardware and software).

Windows on x86/x64 has a memory for system stack ready for each thread. It is reused anytime you do a system call in that thread.

System is re-entrant when multiple independent processes or threads can call system routines at once. In this case each of those threads uses its own system stack. System stack (technically ring 0 stack) is independent from user mode (ring 3) stack.

I can also provide more information, just ask if you wish to know something more specific.

Stack pointer:

  • During normal execution, each thread has its own normal stack and its own stack pointer. It moves down on each call etc.
  • When one particular thread calls operating system routine, current stack pointer value is stored and stack pointer is set to always the same address - system (ring 0) stack.
  • Each thread has its own system (ring 0) stack.
  • When system call ends, original value of stack pointer is restored. So here we actually abandon system stack and go back to normal stack.
  • Size of "normal" stack is set in compiler/linker. It can grow to megabytes. But system stack has a limited static size and you can never change its location or size. It is created exactly so big so any system call can fit there.
  • If one system routine internally calls another system routine, this is detected by hardware and it is called directly, i.e. in that case no special arrangement of stack pointers is made.
0

精彩评论

暂无评论...
验证码 换一张
取 消