Why are user level threads faster than kernel level threads?

07
2014-04
  • vikkyhacks

    I found out that user level threads are a lot faster than kernel level threads but I could not find any convincing example of WHY is user level threads are faster than kernel level threads ? Could someone explain that to me.

    The kernel-level threads are slow and inefficient. For instance, threads operations are hundreds of times slower than that of user-level threads.

    taken from here

  • Answers
  • pjc50

    Kernel-level threads require a context switch, which involves changing a large set of processor registers that define the current memory map and permissions. It also evicts some or all of the processor cache.

    User-level threads just require a small amount of bookkeeping within one kernel thread or process.

    However, the difference isn't big if your threads are predominantly doing I/O operations, as those have to go through the kernel in any case. It's most important if you're trying to implement some kind of simulation with a very large number of independant processes. In that case you need to pay careful attention to what thread synchronisation mechanisms you use, as some of them also go up to the kernel and trigger a context switch.

    http://www.cs.rochester.edu/u/cli/research/switch.pdf "In general, the indirect cost of context switch ranges from several microseconds to more than one thousand microseconds for our workload."

    Edit: user-level threads maintain a stack per-thread, and may or may not save the general-purpose registers depending on the architecture and the clobber rules of its calling convention. It can be as simple as dumping the registers to the stack, jumping to a new address, and popping a few registers, which may be in your cache if that thread was run recently.

    Kernel-level context switches also change the memory map by writing to the TLB, and changing the security level (privilege level or "ring") of the processor. See "Performance Considerations"


  • Related Question

    ubuntu - Why my compiled kernel is so huge?
  • JtR

    I compiled kernel with instructions from here and ended up with a kernel size of 265M. What did I do wrong? Even the initrd image is 50M.


  • Related Answers
  • James

    Are you sure you have not slipped a digit. Why not post the contents of

    ls -lh /boot

    My guess is that you have compiled a monolithic kernel and not used modules at all if the size is right.