3

I have recently learned that Linux kernel threads run in process context.

Why are they run in process context?

Why are they not simply run in a traditional "thread"? (if that even makes sense to ask)

Community
  • 1
  • 1
dtmland
  • 2,136
  • 4
  • 22
  • 45

2 Answers2

9

No it doesn't make sense to ask :) (see here)

Process context merely means the thread is a normal thread, such as the threads you get in processes. Interrupt context just means the thread was started by an interrupt.

Caveat: the following is highly simplified and not be completely accurate:

Interrupts are low level events that cause the CPU to stop what it is doing and execute special code called an interrupt handler (do a context change to the interrupt handler). Interrupts are caused by hardware e.g. a network card signals that a packet has arrived and needs to be read, or by software events e.g. virtual memory uses interrupts to ask the kernel to load a page from disk physical memory, etc..

In modern CPUs interrupts and threads are quite complex, they have priorities, privilege levels, can be individually masked, etc..

Why is it called a process context and not a thread context? I assume this is for historic reasons.

Traditionally Unix, and by extension Linux, did not support threads only processes.

CPUs don't really know about processes and threads, from a CPU point of view they are all execution contexts, the difference between threads and a processes is a function of how the operating system arranges the virtual memory and other OS related attributes (user context, permissions, etc.) of the different execution contexts.

Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
3

A Kernel Thread can be referred to as a context in execution that does not possess a userspace counterpart (unlike other threads/processes in Linux where the userspace process is mapped to a kernel space process). These are generally used as daemons eg. kswapd - the swapper process for evicting virtual memory pages. There is no userspace existence of this process.

Secondly, because these have a definite context associated with itself that can be switched (say the state of registers save on its own stack), the Kernel Threads are schedulable. And, anything that is schedulable can be considered as a "Process context".

On the other hand, interrupts are not schedulable. They occur and execute the interrupt handler spawning its own context.

user31986
  • 1,558
  • 1
  • 14
  • 29