"Good text forwarding---Linux user thread, LWP, kernel thread learning notes"
In modern operating systems, processes support multithreading. A process is the smallest unit of resource management, and a thread is the smallest unit that a program executes. The constituent entities of a process can be divided into two parts: the Thread collection resource set. Threads in a process are dynamic objects and represent the execution of process directives. Resources, including address space, open files, user information, and so on, are shared by threads within the process. Threads have their own private data: program counters, stack spaces, and registers. Why Thread? (the disadvantage of traditional single-threaded processes) 1. In reality, there are many tasks that need concurrent processing, such as server side of database, network server, large capacity computing, etc. 2. The traditional UNIX process is single-threaded, and single-threaded means that the program must be sequential, not concurrent, and can only be run on one processor at a time, and therefore cannot take full advantage of the multiprocessor framework of the computer. 3. If you are using a multi-process approach, you have the following problem: A. Fork a subprocess consumes a lot, and fork is an expensive system call, even when using modern write-time replication (copy-on-write) technology. B. Each process has its own separate address space, and inter-process collaboration requires complex IPC technologies such as message delivery and shared memory. The advantages and disadvantages of multithreading are actually opposites. multithreaded programs (processes) can achieve true parallelism (parallelism), and because of the shared process code and global data, the communication between threads is convenient. It is also a disadvantage because of the thread-sharing process's address space, which can result in competition, so there is a need for some synchronization techniques for data to be accessed by multiple threads in a piece. Three threads-kernel thread, lightweight process, user thread kernel thread kernel thread is the kernel's clone, a 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 [*] 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. SystemThe cost of the system call is relatively high: 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. LWP. JPG 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. Although the user thread LWP is inherently part of the user thread, the LWP line libraries is built on the kernel, and many of the LWP's operations are system-called and therefore inefficient. 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. Uthread1.jpg 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 does not dispatch directly to the user thread process, the kernel scheduler is the same as 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. Uthread2.jpg Summary: In many literatures, lightweight processes are considered threads, which is not entirely true, as you can see from the previous analysis that a lightweight process is a thread only when the user thread is completely composed of lightweight processes.
Original address: http://www.cnitblog.com/tarius.wu/articles/2277.html
"Go" Linux user thread, LWP, kernel thread learning notes