user-level threads and kernel-level threads

According to whether the operating system kernel is aware of threads, threads can be divided into kernel threads and user threads. User-level threads are implemented by threads supported by the application, and the kernel is unaware of the implementation of user-level threads. Kernel-level threads are also known as kernel-supported threads.

user-level thread

User-level threads exist only in user space and are invisible to the operating system. The creation, cancellation, and synchronization and communication functions between threads of such threads do not need to be implemented by system calls. Each thread does not have its own thread context ( the thread context saves the registers used by the thread, data in memory, etc. ).

Advantages of user-level threads

  1. The cost of thread switching is less than that of kernel threads, because the process of saving the thread state and the calling program are only local processes, and there is no context switching.
  2. Each process is allowed to customize its own scheduling algorithm, and thread management (creation, destruction, etc.) is more flexible.

Disadvantages of user threads

  1. When a thread is blocked due to I/O or page fault, if the blocking system call is called, the kernel will block the entire process because it does not know the existence of multiple threads.
  2. Since each thread does not have its own thread context. So in terms of simultaneous execution of threads, only one thread can be running per process at any given time, and only one processor core will be assigned to that process.

kernel-level thread

The management of kernel-level threads (creation and destruction, etc.) is done by the operating system through system calls. The kernel maintains context information for the process and each thread within it, and scheduling is also done on the basis of the kernel's thread-based architecture.

Kernel threads reside in kernel space, which are kernel objects. With kernel threads, each user thread is mapped or bound to a kernel thread. User threads are bound to this kernel thread during their lifetime. Once the user thread terminates, both threads will leave the system. This is called "one-to-one" thread mapping . In addition, there are many-to-one and many-to-many thread mappings.

Advantages of kernel-level threads

  1. In a multiprocessor system, the kernel is capable of executing multiple threads within the same process in parallel.
  2. If a thread in the process is blocked, it can switch other threads in the same process to continue execution.

Disadvantages of kernel-level threads

  1. Even if the CPU switches between multiple threads of the same process, it needs to trap into the kernel, so it's not as fast and efficient as user-level threads.

Guess you like

Origin blog.csdn.net/TABE_/article/details/126394402