跳转到内容

Native POSIX Thread Library

维基百科,自由的百科全书

这是本页的一个历史版本,由Ken Blum留言 | 贡献2008年8月12日 (二) 12:21 新頁面,內容:'''Native POSIX Thread Library''' ('''NPTL''') 能够使 Linux kernel 运行使用 POSIX 线程编写的程序更为有效。 测试表明,NPTL能够成...)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

Native POSIX Thread Library (NPTL) 能够使 Linux kernel 运行使用 POSIX 线程编写的程序更为有效。

测试表明,NPTL能够成功地在IA-32平台2秒内生成 100,000个线程,与其相比,没有NPTL的内核将耗费 15 minutes左右. [1][2]

History

Before the 2.6 version of the Linux kernel, processes were the schedulable entities, and there was no real support for threads. However, it did support a system call - clone() - which creates a copy of the calling process where the copy shares the address space of the caller. The LinuxThreads project used this system call to provide kernel-level thread support (most of the previous pthread implementations in Linux worked entirely in userland). Unfortunately, it had a number of issues with true POSIX compliance, particularly in the areas of signal handling, scheduling, and inter-process synchronization primitives.

To improve upon LinuxThreads, it was clear that some kernel support and a re-written threads library would be required. Two competing projects were started to address the requirement: NGPT (Next Generation POSIX Threads) worked on by a team which included developers from IBM, and NPTL by developers at Red Hat. NGPT was abandoned in mid-2003, at about the same time when NPTL was released.

NPTL was first released in Red Hat Linux 9. Old-style Linux POSIX threading is known for having trouble with threads that refuse to yield to the system occasionally, because it does not take the opportunity to preempt them when it arises, something that Windows was known to do better at the time. Red Hat claimed that NPTL fixed this problem in an article on the Java website about Java on Red Hat Linux 9.[來源請求]

NPTL has been part of Red Hat Enterprise Linux since version 3, and in linux kernel since version 2.6. It is now a fully integrated part of the GNU C Library.

Design

NPTL uses a similar approach to LinuxThreads, in that the primary abstraction known by the kernel is still a process, and new threads are created with the clone() system call (called from the NPTL library). However, NPTL requires specialized kernel support to implement (for example) the contended case of synchronisation primitives which might require threads to sleep and wake again. The primitive used for this is known as a futex.

NPTL is a so-called 1×1 threads library, in that threads created by the user (via the pthread_create() library function) are in 1-1 correspondence with schedulable entities in the kernel (processes, in the Linux case). This is the simplest possible threading implementation.

An alternative to NPTL's 1×1 model is the m×n model where there are typically more userland threads than schedulable entities. In the m×n implementation, the threading library is responsible for scheduling user threads on the available schedulable entities; this makes context switching of threads very fast, as it avoids system calls. However, this increases complexity and the likelihood of priority inversion, as well as suboptimal scheduling without extensive (and expensive) coordination between the userland scheduler and the kernel scheduler.

See also

References