Home >Common Problem >Kernel-level threads run faster
Kernel-level thread:The switching is controlled by the kernel. When the thread switches, it is converted from user mode to kernel mode. After switching, you need to return to user mode from kernel mode; you can make good use of smp, that is, use multi-core CPU. This is what windows threads look like. (Recommended learning: phpstorm)
The switching of user-level thread kernels is controlled by the user-mode program itself. It does not require kernel intervention and reduces the consumption of entering and exiting the kernel state, but it is not very good. Utilizing multi-core CPU, this is generally how Linux pthread is currently used.
The implementation of threads can be divided into two categories: User-Level Thread and Kernel-Level Thread, the latter is also called a kernel-supported thread. or lightweight process. In multi-threaded operating systems, the implementation methods of each system are different. In some systems, user-level threads are implemented, and in some systems, kernel-level threads are implemented.
User thread refers to a thread implemented in a user program without kernel support. It does not depend on the operating system core. The application process uses the thread library to provide creation, synchronization, scheduling and management of threads. function to control the user thread.
No user mode/kernel mode switching is required, it is fast, and the operating system kernel does not know the existence of multi-threads, so blocking one thread will cause the entire process (including all its threads) to block. Since the processor time slice allocation here is based on the process, the execution time of each thread is relatively reduced.
Kernel thread: Created and destroyed by the operating system kernel. The kernel maintains process and thread context information and thread switching. If a kernel thread is blocked due to I/O operations, it will not affect the operation of other threads. Windows NT and 2000/XP support kernel threads.
User threads run on an intermediate system. There are currently two ways to implement intermediate systems, namely runtime system and kernel control thread.
The "runtime system" is essentially a collection of functions used to manage and control threads, including creation, cancellation, thread synchronization and communication functions, and scheduling functions. These functions reside in user space and serve as the interface between user threads and the kernel.
User threads cannot use system calls. Instead, when the thread needs system resources, the request is sent to the runtime, and the latter obtains system resources through the corresponding system calls.
Kernel control thread: The system assigns several lightweight processes (LWP) to the process. LWP can obtain services provided by the kernel through system calls, and user threads in the process can be associated with LWP through multiplexing. So as to obtain the services of the kernel.
The following is the difference between user-level threads and kernel-level threads:
(1) Kernel-supported threads are perceptible to the OS kernel, while user-level threads are OS kernel aware Insensible.
(2) The creation, cancellation and scheduling of user-level threads do not require the support of the OS kernel and are handled at the language (such as Java) level; while the creation, cancellation and scheduling of kernel-supported threads require Support is provided by the OS kernel, and is largely the same as process creation, destruction, and scheduling.
(3) When a user-level thread executes a system call instruction, it will cause the process to which it belongs to be interrupted, while when a kernel support thread executes a system call instruction, it will only cause the thread to be interrupted.
(4) In a system with only user-level threads, CPU scheduling is still based on the process. Multiple threads in a running process are controlled by the user program to run in rotation; if there is kernel support In a thread system, CPU scheduling is based on threads, and the thread scheduler of the OS is responsible for thread scheduling.
(5) The program entity of user-level threads is a program running in user mode, while the program entity of kernel-supported threads is a program that can run in any state.
The above is the detailed content of Kernel-level threads run faster. For more information, please follow other related articles on the PHP Chinese website!