6

Could someone help clarify my understanding of kernel threads. I heard that, on Linux/Unix, kernel threads(such as those of system calls) get executed faster than user threads. But, aren't those user threads scheduled by kernel and executed using kernel threads? could someone please tell me what is the difference between a kernel thread and a user thread other than the fact that they have access to different address spaces. what are other difference between them? Is it true that on a single processor box, when user thread is running, kernel will be suspended?

Thanks in advance,

Alex

1 Answer 1

9

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).
5
  • so on a single CPU, a raw CPU cycles are time-sliced into many execution queues for kernel threads. a few of these queues are further sliced for running numerous user-threads. Am I correct?
    – techie11
    Jul 16, 2013 at 18:57
  • The hierarchy you're assuming (user threads under kernel threads) does not exist. The same hierarchy of process priorities is used for both user tasks and kernel threads. Additionally, at each scheduling quantum, the CPU picks an eligible task to run — so long as there is at least one runnable process, the CPU is not idle.
    – user149341
    Jul 16, 2013 at 19:35
  • You missed the most important one - when an I/O interrupt to a driver makes a waiting thread ready. This is the main reason for using a preemptive mititasking kernel - good I/O performance. Jul 16, 2013 at 20:50
  • Thanks. There was such hierarchy in Solaris 8 and earlier versions. several user threads were multiplexed on a single kernel thread. But since Solaris 9. a single user thread corresponds to a single kernel thread.
    – techie11
    Jul 16, 2013 at 21:29
  • "Some kernel threads have higher than default priority (up to realtime priority in some cases)", does it mean kernel thread have less latency the user threads ? If we declare userspace thread as SCHED_FIFO with a bigger priority won't it be scheduled before the kernel thread ?
    – ransh
    Dec 19, 2016 at 15:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.