Differences between processes, lightweight processes, kernel threads, and user threads

Source: Internet
Author: User

In modern operating systems, processes support multithreading. A process is the smallest unit of resource management, while a thread is the smallest unit of program execution. A process consists of two parts: a thread collection resource set. A thread in a process is a dynamic object and represents the execution of process commands. Resources, including address space, opened files, and user information, are shared by threads in the process.

The thread has its own private data: Program counters, stack space, and registers.

Why thread? (Disadvantages of traditional single-threaded processes)

1. In reality, there are many tasks that need to be processed concurrently, such as database servers, network servers, and large-capacity computing.

2. traditional UNIX processes are single-threaded. A single thread means that the program must be executed sequentially and cannot be concurrently. It can only run on one processor at a time, therefore, computers with multi-processor frameworks cannot be fully utilized.

3. If the multi-process method is used, the following problems occur:
A. Fork a sub-process consumes a lot, and fork is an expensive system call, even if the modern copy-on-write technology is used.
B. Each process has its own address space. inter-process collaboration requires complex IPC technologies, such as message passing and shared memory.

Advantages and disadvantages of Multithreading

The advantages and disadvantages of multithreading are actually the unity of opposites.

Multi-threaded programs (processes) can obtain real parallelism, and the communication between threads is convenient because the process code and global data are shared. Its disadvantage is that the thread shares the address space of the process, which may lead to competition. Therefore, some synchronization technologies are required for the data to be accessed by multiple threads in a single thread.

Three threads: Kernel thread, lightweight process, and user thread

Kernel thread

The kernel thread is the separation of the kernel, which can process a specific thing. This is particularly useful in processing asynchronous events such as Asynchronous Io. The use of kernel threads is cheap. The only resource used is the space for storing registers during kernel stack and context switching. A multi-threaded kernel is called a multi-threads kernel ).

Lightweight Process [*]

A lightweight thread (lwp) is a user thread supported by the kernel. It is an advanced abstraction based on Kernel threads. Therefore, lwp is available only when kernel threads are supported first. Each process has one or more lwps, and each lwp is supported by one kernel thread. This model is actually a one-to-one thread model mentioned in the dinosaur book. In this operating system, lwp is the user thread.

Since each LWP is associated with a specific kernel thread, each LWP is an independent Thread Scheduling unit. Even if an LWP is blocked in a system call, the execution of the entire process is not affected.

Lightweight processes have limitations. First, most LWP operations, such as creation, analysis, and synchronization, require system calls. The system call cost is relatively high: you need to switch between usermode and kernel mode. Secondly, each LWP requires support from a kernel thread. Therefore, LWP consumes kernel resources (the stack space of the kernel thread ). Therefore, a system cannot support a large number of LWP.


Note:

1 The term LWP is derived from SVR4/MP and Solaris 2.x.

2. Some systems call LWP a virtual processor.

3. The reason for this is that, with the support of kernel threads, LWP is an independent scheduling unit, just like a common 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 thread library is built on the kernel. Many LWP operations must be called by the system, so the efficiency is not high. Here, the user thread refers to the thread library fully established in the user space. The creation, synchronization, destruction, and scheduling of user threads are completely completed in the user space without the help of the kernel. Therefore, such thread operations are extremely fast and low-consumption.

It is an initial user thread model. It can be seen that the process contains threads and user threads are implemented in the user space. The kernel does not directly schedule user threads, the scheduling object of the kernel is the same as that of the traditional process. The kernel does not know the existence of the user thread. Scheduling between user threads is implemented by the thread library implemented in the user space.

This model corresponds to the many-to-one thread model mentioned in the dinosaur book. Its disadvantage is that if a user thread is blocked in system calls, the whole process will be blocked.

Enhanced User thread-user thread + lwp

This model corresponds to the many-to-many model in the dinosaur book. The user thread library is still completely built in the user space, so the user thread operations are still very cheap, so you can create any number of user threads required. The operating system provides lwp as a bridge between the user thread and the kernel thread. Lwp is also the same as previously mentioned. It has kernel thread support and is the scheduling unit of the kernel. In addition, the system calls of user threads must be implemented through lwp, therefore, the blocking of a user thread in a process does not affect the execution of the whole process. The user thread library associates established User threads with lwp. The number of lwp threads is not necessarily the same as that of user threads. When the kernel is scheduled to an lwp, the user Thread associated with the lwp is executed.

Summary:

Many documents have considered that a lightweight process is a thread. In fact, this statement is not completely correct. From the previous analysis, we can see that only when the user thread is completely composed of a lightweight process, it can be said that a lightweight process is a thread.

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.