Copy pasted from [this] (https://www.tutorialspoint.com/operating_system/os_multi_threading) link:

  • Thread switching does not require Kernel mode privileges.
  • User level threads are fast to create and manage.
  • Kernel threads are generally slower to create and manage than the user threads.
  • Transfer of control from one thread to another within the same process requires a mode switch to the Kernel.

I never came across these points while reading standard operating systems reference books. Though these points sound logical, I wanted to know how they reflect in Linux. To be precise :

  1. Can someone give detailed steps involved in context switching between user threads and kernel threads, so that I can find the step difference between the two.

  2. Can someone explain the difference with actual context switch example or code. May be system calls involved (in case of context switching between kernel threads) and thread library calls involved (in case of context switching between user threads).

  3. Can someone link me to Linux source code line (say on github) handling context switch.

  4. I also doubt why context switch between kernel threads requires changing to kernel mode. Aren't we already in kernel mode for first thread?


I think you are misunderstanding the used terminology.

In the context of the linked tutorial and many operating system lectures/books (example) the classic distinction between user and kernel threads refers to:

  • kernel threads - threads that are managed by the kernel, but are running user space programs
  • user (level) threads (a.k.a. 'fibers') - threads the kernel doesn't know anything about and that are completely managed by the application (or a library the application uses, such as pth) in userspace

The purpose of looking at user threads usually is to illustrate cooperative multitasking, showing the advantage of saving a real kernel-style context switch and introduce students to context switching.

In contrast to that, in the context of the Linux kernel, the terms are defined differently:

  • kernel threads - lightweight threads created by the kernel for kernel tasks that are running in kernel space
  • user threads - application threads that are managed by the kernel and are running in user space; i.e. threads created by pthread_create() or similar library function or by the more low level clone() syscall

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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