Operating systems: User-level threads and kernel-level threads

Source: Internet
Author: User
Tags switches

Http://www.cnblogs.com/yxzfscg/p/4758728.html

Three threads-kernel thread, lightweight process, user thread

Kernel threads

A kernel thread is a clone of the kernel, and a single clone can handle a specific thing. This is particularly useful when dealing with asynchronous events such as asynchronous IO. The use of kernel threads is inexpensive, and the only resource used is the space for storing registers when the kernel stack and context switches. Multithreading-enabled cores are called multithreaded cores (multi-threads kernel).

Lightweight process [*]

A lightweight thread (LWP) is a user thread supported by the kernel. It is a high-level abstraction based on kernel threads, so you can have an LWP only if you support kernel threads first. Each process has one or more lwps, and each LWP is supported by a kernel thread. This model is actually a pair of thread models mentioned in the Dinosaur book. In this implementation of the operating system, the LWP is the user thread.

Since each LWP is associated with a particular kernel thread, each LWP is a separate thread dispatch unit. Even if an LWP is blocked in a system call, it does not affect the execution of the entire process.

Lightweight processes have limitations. First, most LWP operations, such as Setup, destructor, and synchronization, require system calls. System calls are relatively expensive: you need to switch between user mode and kernel mode. Second, each LWP needs to have a kernel thread support, so LWP consumes kernel resources (the stack space of kernel threads). Therefore, a system cannot support a large number of LWP.

Note:

1. The terms of LWP are borrowed from SVR4/MP and Solaris 2.x.

2. Some systems refer to LWP as a virtual processor.

3. The reason that this is called a lightweight process may be that, with the support of kernel threads, LWP is a separate dispatch unit, just like a normal process. So the biggest feature of LWP is that each LWP has a kernel thread support.

User thread

Although LWP is essentially a user thread, the LWP line libraries is built on top of the kernel, and many of the operations of LWP are system-called, so the efficiency is not high. The user thread here refers to the line libraries, which is completely built in the user space, the user thread is built, synchronized, destroyed, and the schedule is completely completed in the user space without the help of the kernel. So the operation of this thread is extremely fast and low-consumption.

Is the initial user threading model, from which it can be seen that the process contains threads, the user thread is implemented in user space, the kernel is not directly to the user thread process scheduling, the kernel's scheduling object and the traditional process, or the process itself, the kernel does not know the existence of the user thread. Scheduling between user threads is implemented by a line libraries implemented in user space.

This model corresponds to the many-to-one-thread model mentioned in the Dinosaur book, with the disadvantage that if a user thread is blocked in the system call, the entire process will be blocked.

Enhanced user thread-user thread +LWP

This model corresponds to many-to-many models in dinosaur books. User-line libraries is still completely built into user space, so the user thread is still very inexpensive, so you can create any user thread that you need. The operating system provides the LWP as a bridge between the user thread and the kernel thread. The LWP is also the same as mentioned earlier, with kernel thread support, the kernel's dispatch unit, and the user thread's system call going through LWP, so the blocking of a user thread in the process does not affect the execution of the entire process. The User Line Libraries associates the established user thread to the LWP, and the number of LWP and user threads does not necessarily match. When the kernel is dispatched to an LWP, the user thread associated with the LWP is executed at this time.

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.

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

Http://www.cnitblog.com/tarius.wu/articles/2277.html

Operating systems: User-level threads and kernel-level 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.