User thread and kernel thread, user thread kernel thread
1. kernel-level thread: switch is controlled by the kernel. When the thread is switched, the user State is converted to the kernel state. After switching, the user State should be returned from the kernel state. smp can be used well, that is, multi-core cpu can be used. This is the case for windows threads. Context switching is required when the user State is converted to the kernel state, which is time-consuming because of the storage and loading of register values, failure and loading of memory cache, and interruption of program execution.
2. the user-level thread kernel switching is controlled by the user State program. kernel switching does not require kernel interference, which reduces the consumption of the incoming and outgoing kernel states, but does not make good use of the multi-core Cpu, at present, Linux pthread generally does this.
Thread implementation can be divided into two types: User-Level Thread and Kernel-Level Thread. The latter is also called a Thread or lightweight process supported by the Kernel. In a multi-threaded operating system, 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.
A user thread is a thread implemented in a user program without kernel support. It does not depend on the operating system core, that is, it does not need to be called by the system. The application process uses the thread library to provide functions for creating, synchronizing, scheduling, and managing threads to control user threads. User-mode/Core-mode switching is not required. The speed is fast, and the operating system kernel does not know the existence of multiple threads. Therefore, a thread blocking will cause the entire process (including all its threads) to be blocked, the thread does not have the interruption caused by time slice, so the CPU usage is self-conscious. Because the time slice allocation of the processor here is the basic unit of the process, the execution time of each thread is relatively reduced.
Kernel thread: It is created and abolished by the operating system kernel. Kernel maintenance process and thread context information and thread switching. A kernel thread is blocked due to I/O operations and does not affect the running of other threads. Windows NT and 2000/XP support kernel threads.
The user thread runs on an intermediate system. Currently, there are two methods to implement the intermediate System: the Runtime System and the kernel control thread. The "runtime system" is essentially a set of functions used to manage and control threads, including functions for creating, revoking, synchronizing and communicating threads, and scheduling functions. These functions reside in the user space as interfaces between the user line and the kernel. User threads cannot use system calls. Instead, when the thread needs system resources and sends requests to the runtime, the latter obtains system resources through corresponding system calls. Kernel control thread: The system is distributing several lightweight processes (LWP) to processes. LWP can obtain the services provided by the kernel through system calls. The user threads in the process can be associated with LWP through reuse, to obtain the kernel service.
The following are the differences between user-level threads and kernel-level threads:
(1) Kernel support threads are perceptible to the OS kernel, while user-level threads are imperceptible to the OS kernel.
(2) creation, cancellation, and scheduling of user-level threads do not require the support of the OS kernel. They are processed at the language level (such as Java; the kernel supports thread creation, cancellation, and scheduling. It must be supported by the OS kernel, which is basically the same as Process Creation, revocation, and scheduling.
(3) When a user-level thread executes a System Call Command, the process to which it belongs will be interrupted. When the kernel supports the thread to execute the System Call Command, only this thread will be interrupted.
(4) In a user-level thread-only system, CPU scheduling is based on processes. Multiple Threads in a running process are rotated by the user program to control threads; in a system with kernel-supported threads, CPU scheduling is based on threads, and the OS thread scheduling program is responsible for thread scheduling.
(5) user-level thread program entities are programs running in the user State, while program entities supporting threads in the kernel are programs that can run in any State.
Advantages of kernel threads:
(1) When multiple processors exist, multiple threads of a process can be executed simultaneously.
Disadvantages:
(1) scheduling by the kernel.
Advantages of user processes:
(1) The scheduling of threads does not require direct kernel participation, and the control is simple.
(2) It can be implemented in an operating system that does not support threads.
(3) The cost of thread management, such as thread creation and destruction and thread switching, is much lower than that of kernel threads.
(4) Each process is allowed to customize its own scheduling algorithm, and thread management is flexible. This is the difference between the Management Program and the kernel thread.
(5) threads can use more tablespaces and stack spaces than kernel-level threads.
(6) only one thread can be running in the same process. If one thread is blocked by using a system call, the whole process will be suspended. In addition, page invalidation also causes the same problem.
Disadvantages:
(1) resource scheduling is performed according to the process. Under multiple processors, threads in the same process can only be reused in a time-sharing manner under the same processor.