I saw some SO questions come with a conclusion: Each process has its own kernel stack. But how about this code?
main() { while(1) ; }
Or how about this one?
main() { }
Whether does this program also have a corresponding kernel stack?
More: For the 1st code above, I tested it like below. Press Ctrl+C to stop it, and see that system time is less than 1ms as below.
I think what I want to know is whether a user space program’s execution path must go through kernel space? For my example code, I think the process itself does not go through kernel space when it executes, is this right?
$ time ./a.out ^C real 0m24.953s user 0m24.942s sys 0m0.000s
Advertisement
Answer
Application code is loaded (from executable file) into memory by the kernel. But kernel doesn’t perform desassembling. So, kernel cannot detect, whether code is short or not, whether it uses system calls or not, and so on.
Because of that, for any application kernel needs to create full execution context. So, allocating kernel stack is needed in any case.
Note also, that system call is not the only case when kernel executes code in the context of the application’s process. Process’s preemption, exception handling is also performed by the kernel, and requires kernel stack.