user threads v.s. kernel threads user threads v.s. kernel threads multithreading multithreading

user threads v.s. kernel threads


I heard that, on Linux/Unix, kernel threads(such as those of system calls) get executed faster than user threads.

This is a largely inaccurate statement.

  • Kernel threads are used for "background" tasks internal to the kernel, such as handling interrupts and flushing data to disk. The bulk of system calls are processed by the kernel within the context of the process that called them.

  • Kernel threads are scheduled more or less the same way as user processes. Some kernel threads have higher than default priority (up to realtime priority in some cases), but saying that they are "executed faster" is misleading.

Is it true that on a single processor box, when user thread is running, kernel will be suspended?

Of course. Only one process can be running at a time on a single CPU core.

That being said, there are a number of situations where the kernel can interrupt a running task and switch to another one (which may be a kernel thread):

  • When the timer interrupt fires. By default, this occurs 100 times every second.
  • When the task makes a blocking system call (such as select() or read()).
  • When a CPU exception occurs in the task (e.g, a memory access fault).