User Threads and Kernel threads

Source: Internet
Author: User

1. Kernel-level thread: The switch is controlled by the kernel, and when the thread switches, the user state is converted to the kernel state. The switch is completed to return the user state from the kernel state, and the SMP can be used to make use of the multi-core CPU. This is the case with Windows threads. When the user state is converted to the kernel state, it is time-consuming to switch the context, because the storage load of register value, the invalidation and loading of memory cache, the execution of interrupt program, etc.


2. User-level thread core switching by the user-state program to control the kernel switch, do not need the core interference, less in and out of the kernel state consumption, but not a good use of multi-core CPU, the current Linux pthread generally do so.


Thread implementations can be divided into two categories: User-level threads (user-level thread) and kernel thread threads (kernel-level thread), which are also known as kernel-supported threads or lightweight processes. In multi-threaded operating system, the implementation of each system is not the same, in some systems to implement the user-level threads, and some systems to implement the kernel-level threads.


A user thread is a thread implemented in a user program that does not require kernel support, and is not dependent on the core of the operating system, i.e. no system calls are required. The application process leverages line libraries to provide functions to create, synchronize, schedule, and manage threads to control user threads. No need for user-state/kernel-mindset switching, fast, the operating system kernel does not know the existence of multithreading, so a thread blocking will make the entire process (including all its threads) blocked, the thread does not exist the time slice caused by the interruption, so let out the CPU rely on consciousness. Because the processor time slice allocation here is in the process as the basic unit, each thread executes relatively less time.


kernel thread: created and revoked by the operating system kernel. The kernel maintains the context information of processes and threads, as well as thread switching. A kernel thread is blocked due to I/O operations and does not affect the operation of other threads. Windows NT and 2000/XP support kernel threads.

the user thread runs on top of an intermediate system. There are two ways in which the intermediate system is implemented, namely the runtime system and the kernel control thread. The runtime system is essentially a collection of functions for managing and controlling threads, including creation, revocation, thread synchronization and communication functions, and scheduled functions. These functions reside in user space as an interface between the user thread and the kernel. The user thread cannot use a system call, but when the thread needs system resources, the request is routed to the runtime, which obtains the system resources through the appropriate system calls. Kernel Control Threads: The system is assigned to several lightweight processes (LWP) in the process, and LWP can obtain the services provided by the kernel through system calls, while the user threads in the process can be reused to correlate to the LWP to obtain the service of the kernel.

The following are the differences between a user-level thread and a kernel-level thread:

(1) Kernel support threads are perceived by the OS kernel, while user-level threads are not perceived by the OS kernel.

(2) User-level thread creation, revocation, and scheduling does not require the support of the OS kernel, is processed at the level of language (such as Java), while kernel support thread creation, revocation and scheduling requires the OS kernel to provide support, and process creation, revocation and scheduling is generally the same.

(3) When a user-level thread executes a system invoke instruction, it causes its owning process to be interrupted, while the kernel support thread executes the system call instruction, which causes the thread to be interrupted only.

(4) in a system with only a user-level thread, the CPU is dispatched as a process, and multiple threads in the running process are run by the user program, and in a system with kernel support threads, CPU scheduling is thread-based, and the thread scheduler of the OS is responsible for thread scheduling.

(5) The program entity of a user-level thread is a program that runs under a user-state, while a kernel-enabled program entity is a program that can run in any state.


Benefits of Kernel Threading:

(1) When there are multiple processors, multiple threads of a process can execute concurrently.

Disadvantages:

(1) dispatched by the kernel.

advantages of the user process:

(1) The thread scheduling does not require the kernel to participate directly, the control is simple.

(2) can be implemented in an operating system that does not support threading.

(3) thread management, such as creating and destroying threads, thread switching costs, is much less expensive than kernel threads.

(4) Allow each process to customize its own scheduling algorithm, thread management is more flexible. This is the difference between a kernel thread and a hypervisor that must be written by itself

(5) Threads can take advantage of more table space and stack space than kernel-level threads.

(6) Only one thread is running in the same process, and if one thread is blocking with a system call, the entire process will be suspended. Also, a page failure can cause the same problem.

Disadvantages:

(1) Resource scheduling according to the process, multiple processors, the same process of the thread can only be used in the same processor time-sharing reuse

User Threads and Kernel threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.