Home > OS >  Does thread occupies its independent kernel space?
Does thread occupies its independent kernel space?

Time:12-11

This is what I have understand so far...

  1. Every process has its own kernel stack(space)
  2. Thread has independent stack memory and share the other (heap code etc...)
  3. What are inside in the Kernel stack is some formal information for its process' later context switch

Here is the part where I get confused....
If Thread only has its own stack memory, how does OS can manage thread's context switch? If process A has 10 threads, all those threads' information is written in process A's kernel space? wouldn't be cause a lack of memory in kernel space when there are too many threads?

Please let me know what I am missing here.
Thanks.

CodePudding user response:

  1. Every process has its own kernel stack(space)

Yes.

  1. Thread has independent stack memory and share the other (heap code etc...)

Yes, roughly.

  1. What are inside in the Kernel stack is some formal information for its process' later context switch

No. The kernel stack associated with a thread is used as a call stack during the execution of syscalls performed on behalf of that thread. As far as I am aware, it is not involved in context switching between threads, and not directly in switches from user mode to kernel mode.

If Thread only has its own stack memory, how does OS can manage thread's context switch?

The same way that it manages context switches among processes. The kernel maintains the information needed to do so (which is not particularly much on a per-thread basis), albeit not on threads' kernel stacks as far as I am aware. But this is perhaps splitting hairs over terminology -- you seem to have identified "kernel stack" with "kernel space", but the kernel maintains more per-thread data than just a kernel stack.

If process A has 10 threads, all those threads' information is written in process A's kernel space? wouldn't be cause a lack of memory in kernel space when there are too many threads?

I'm not sure you have quite the right view of things, but yes, the number of threads that can be alive simultaneously is limited by available resources. The details are a property of the kernel and threading implementations. There can be both per-process and global limits.

In practice, processes running on Linux and using native threads can have thousands of threads if available memory (physical and virtual) is sufficient. Kernel stacks are usually only a few kB each; these are not ordinarily the limiting factor.

  • Related