Advantages and disadvantages of user threads and kernel threads

Source: Internet
Author: User

Transfer from http://col1.blog.163.com/blog/static/1909775192012719114033352/

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. Windows threads and Linux pthread are doing this in general.

2. User-level thread core switching by the user-state program to control the kernel switching, without the need for core interference, less in and out of the kernel state consumption, but not a good use of multi-core CPU,

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 operating system core, and the application process uses line libraries to provide functions to create, synchronize, dispatch, and manage threads to control the user thread. No need for user-state/kernel-mindset switching, fast, operating system kernel does not know the existence of multithreading, so a thread blocking will make the entire process (including all its threads) blocked. 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, the CPU is dispatched in a thread. The thread scheduler of the OS is responsible for the scheduling of threads.

(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.

(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

Advantages and disadvantages of 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.