HPlogo HP-UX Process Management: White Paper > Chapter 1 Process Management

Thread Models

» 

Technical documentation

Complete book in PDF

 » Table of Contents

Industrywide, threads are implemented according to four distinct models:

  • User-space threads ("many [threads] to one [process]" (M x 1))

  • Kernel-space threads ("one to one" (1 x 1))

  • user-space and kernel-space threads ("many to many" (M x N))

  • POSIX threads (pthreads), which can be used with any other model

User-Space Threads (M x 1)

User-space threads (M x 1, based on POSIX draft 4 threads) are created, terminated, synchronized, scheduled, and so forth using interfaces provided by a threads library. Creation, termination, and synchronization operations can be performed extremely fast using user-space threads.

Because user-space threads are not directly visible to the kernel (which is aware only of the overriding process containing the user-space threads), user-space threads (M x 1) require no kernel support. As shown in the following figure, all thread management is done in user space, so there is no overhead associated with crossing the kernel boundary.

Figure 1-7 User-space (M x 1) threads for an application (process)

[User-space (M x 1) threads for an application (process)]

If one thread blocks, the entire process blocks. When this happens, the benefit of threads parallelism is lost. Wrappers around various system calls can reduce some of the blocking, but at a cost to performance.

User-space threads have been on HP-UX since release 9.0 with DCE and on all 10.0 systems.

Kernel-Space Threads (1 x 1)

With kernel threads (1 thread to one process, or 1 x 1), each user thread has a corresponding kernel thread. Thus, there is full kernel support for threads.

Each thread is independently schedulable by the kernel, so if one thread blocks, others can still run.

Creation, termination, and synchronization can be slower with kernel threads than user threads, since the kernel must be involved in all thread management. Overhead may be greater, but more concurrency is possible using kernel threads, even with a uniprocessor system. As a result, total application performance with kernel-space threads surpasses that of user-space threads.

Note, however, that developers must be more careful when creating large amounts of threads, as each thread adds more weight to the process and more overhead to the system

Figure 1-8 Kernel-space (1 x 1) threads for an application (process)

[Kernel-space (1 x 1) threads for an application (process)]

User-Space and Kernel-Space Threads (M x N)

The model of maximal flexibility in the number of threads to processes (M x N) provides the developer with both user-space and kernel-space threads.

The M x N model allows threads to be bound or unbound from kernel threads. Users can use unbound user space threads to take advantage of thread management occurring in user space. Bound threads enable use of the independent kernel scheduling provided for by kernel threads, as well as the true parallelism capabilities (both logical and physical) of an MP machine.

Figure 1-9 User-space and kernel-space (M x N) threads for an application (process)

[User-space and kernel-space (M x N) threads for an application (process)]

The combination of user-space and kernel-space threads allow for extremely fast and efficient thread creation, termination, synchronization, and context switching, and thus, better performance and more system throughput than either 1 x 1 or M x 1 model. Developers need not worry about additional threads adding weight to the process or overhead to the system, as in the user-space threads (1 x 1) model. Further, blocking does not require the kernel to context switch to another process; another thread in the process will execute next.

Because the M x N model is the most powerful and flexible for programmers, it is also the most complex. Debugging can be difficult.

POSIX Threads (pthreads)

HP-UX release 10.30 implements the 1 x 1 (kernel-space threads) version of IEEE Portable Operating System Interface standard threads, (POSIX 1003.1c, based on final draft 10). Pthreads includes functions and application programming interfaces (APIs) to support multiple flows of control (threads) within a process. Using pthreads enables developers to create source-code portable applications.

For details on pthread functions, attributes, stack information, and so forth, consult the pthread.h file.

NOTE: The HP-UX DCE version of threads (M x 1, or user-space threads) complies with POSIX draft 4. However, neither 1 x 1 nor M x N model implementations will be binary or source compatible. In addition, HP also provides extensions to the POSIX.1c threads programming environment to equip the programmer with additional control and powerful features previously unavailable. These extensions, however, are not portable on different platforms. Do not rely on them for writing portable applications!