Linux POSIX Thread Libraries Summary

Source: Internet
Author: User
Tags posix

Due to historical reasons, the2.5.x previous Linux did not provide kernel-level support for pthreads, so the pthreads implementation on Linux can only take the form of N:1, also known as the library implementation.

Currently, Pthreads is implemented in 3 ways:

(1) First, many to one, that is, the library implementation.

(2) The second type, 1:1 mode.

1:1 mode is suitable for CPU-intensive machines. We know that the process (thread) is blocked in the run by waiting for some kind of resource, either an I/O resource or a CPU. The 1:1 mode on multi-CPU machines is a good choice. So the advantage of 1:1 is that it gives full play to the advantages of SMP.
This model also has its drawbacks. Because the OS establishes a kernel thread for each thread, it causes a large overhead of kernel-level memory space (up to 1G of the 4G virtual storage space of the IA32 machine). The second disadvantage is that, in the traditional sense, the operation on mutex mutexes and condition variables requires a kernel state, because the OS is responsible for scheduling and it is fully responsible for the transition of the thread. As we'll see later, the Linux NPTL library avoids this disadvantage, and its mutex and condition variable operations are done in the user state.

(3) The third type,M:n mode.

This model attempts to have the advantages of the above 2 modes. It requires a layered model. For example, there are 4 threads in a process where each of the 2 threads corresponds to a kernel thread. In this way, theOS knows the existence of 2 entities and is responsible for their scheduling. And there are 2 threads within an entity , and the scheduling between the 2 threads is the OS without interference or knowledge.
Simply put,M:n model OS-level scheduling, similar to the 1:1 model, inter-thread scheduling, with the N:1 model similar.
The advantage is that the advantage of SMP can be exploited in the process, and the consumption of system space memory can be saved, and the environment switch is mostly done in the user state.
The disadvantage is obvious: complex.

Linux threads are implemented through processes. Linux Kernel provides a clone () system call for process creation, and the parameters of clone include such as CLONE_VM, Clone_files, Clone_sighand, and so on. With the parameters of Clone (), the newly created process, also known as LWP (lightweight process), shares memory space with the parent process, file handles, signal processing, etc., thus achieving the same purpose as creating threads.

Prior to Linux 2.6, Linux kernel had no real thread support, and some of the thread libraries were based on the user space package on clone (), so typically in signal processing, process scheduling ( Each process requires an additional scheduling thread) and there are some problems in synchronizing shared resources between multiple threads. the line libraries of Linux 2.6 is called NPTL (Native POSIX Thread Library). POSIX Thread (pthread) is a programming specification that provides a good cross-platform feature for multithreaded programs developed through this specification. Although it is a process-based implementation, the new version of NPTL creates threads with very high efficiency. Some tests show that the NPTL-based kernel requires 2 seconds to create 100,000 threads, while a kernel without NPTL support takes up to 15 minutes.

In Linux, each thread has a task_struct. Threads and processes can dispatch their schedules using the same schedule. There is no difference between the LWP and the process in the kernel perspective, but only the sharing of resources. If the exclusive resource is HWP, the shared resource is the LWP. The implementation of the NPTL implemented in the real kernel is implemented in kernel with the addition of Futex (fast userspace mutex) support for handling sleep and wake between threads. Futex is an efficient algorithm for mutually exclusive access to shared resources. Kernel plays a role in arbitration, but it is usually done by the process itself. NPTL is a 1x1 threading model, that is, a thread for an operating system scheduling process, the advantage is very simple. Some other operating systems, such as Solaris, are MXN, and m corresponds to the number of threads created, and n corresponds to the entities that the operating system can run. (N<M), the advantage is that thread switching is fast, but the implementation is slightly more complex.

Note:

(1) Pthread line Libraries--NPTL (Native POSIX threading Library)

In the 1:1 core threading model, every thread (and also a book called LWP) created by an application is managed directly by a core thread. each core thread is tuned to the system CPU by the OS kernel,

Therefore, all threads work in the system contention scope: Thethreads compete directly with other threads in system-wide.

(2) Ngpt (Next Generation POSIX Threads)

The N:M hybrid threading model provides level two control that maps user threads to the system's scheduler for parallelism, which is called a lightweight process (lwp:light weight processes), LWP

Another one by one maps to the core thread. As shown in. Each core thread is tuned to the system CPU by the OS kernel, so all threads work in the "System race range".

Linux POSIX Thread Libraries Summary

Related Article

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.