Section 5.16. POSIX Threads


5.16. POSIX Threads

A thread is the most basic independent flow of control that operates within the address space of a process. Although this characteristic is consistent across UNIX systems, the architecture is not, which is why standards have been created. The universal standard for implementing threads within the UNIX world today is POSIX 1003.1, better known as pthreads. POSIX defines a set of strict standardized APIs that operating system vendors must adhere to and provide for application programmers. The idea here is to enable programmers to make their applications portable from one machine to the next. As of the writing of this book, the newest version of the POSIX thread library supported on Linux is the Native POSIX Thread Library (NPTL) developed by Ulrich Drepper.

5.16.1. Linux Versus AIX Thread Models

AIX supports both the M:N and 1:1 threading models, configurable by the user. The M:N model means the operating system implements N kernel threads to service M user threads. The 1:1 model means the system implements one kernel thread for every user thread. Linux NPTL threads only support the 1:1 model. This may affect process scheduling and performance tuning considerations.

Additionally, in AIX and Linux, thread and process characteristics are somewhat obscured. In AIX, a thread is a schedulable entity within a process that has properties that ensure its independent flow of control. These properties include the following:

Stack

Scheduling properties (such as policy or priority)

Set of pending and blocked signals

Some thread-specific data (such as an ERRNO)

In Linux, a thread is considered a "lightweight process" (LWP), which has properties of a process but is not as heavy. Those properties include the following:

Code (or "text")

Data

Stack

File I/O

Signal tables

Finally, threads are created architecturally different in AIX than in Linux. Traditionally, to protect the operating system, on most UNIX-like environments, kernel space threads and user space threads are implemented separately within strict processing boundaries.

5.16.2. Kernel Space Threads

Kernel-supported threads fall into two classes. In a "pure" kernel-supported system, the kernel is responsible for scheduling all threads. Systems in which the kernel cooperates with a user-level library to do scheduling are known as two-level, or hybrid, systems. Typically, the kernel schedules LWPs, and the user-level library schedules threads onto LWPs.

Both AIX and Linux support this thread model. Linux implementation of kernel threads is somewhat less strict than AIX. Linux uses the clone() system call to create threads. In the past, this raised questions as to whether these were truly kernel threads. After version 2.6, however, the clone() system call was modified under Linux to adhere strictly to the POSIX standards of a kernel thread. Linus Torvalds and other Linux kernel developers consider this model to be more simplistic and have done a pretty good job of making kernel-level context switches between threads efficient.

5.16.3. User-Space Threads

User-space threads live with limited support from the kernel; they maintain all of their state in user space. In addition, user-space threads have their own user scheduler independent of the kernel scheduler. The user scheduler makes it possible for user-space threads to schedule themselves on a kernel thread to execute code. Different implementations of user-space threads exist. On AIX, user-space threads are implemented to run with a ratio of eight user-space threads to one kernel thread by default.

Linux does not support user-space threads and does not anticipate supporting it in the future.

5.16.4. AIX to Linux POSIX Threads Cross-Reference

The POSIX threads implementations of AIX and Linux have little differences. As long as the AIX multithreaded application does not use nonportable POSIX extensions, porting should be straightforward. Tables 5-14 and 5-15 list the POSIX thread attribute, data type, and API differences between AIX and Linux, respectively.

Table 5-14. AIX and Linux POSIX Threads Comparison: Attributes

AIX

Linux

Comment

pthread_t

pthread_t

Identifies a thread.

pthread_attr_t

pthread_attr_t

Identifies a thread attribute object.

pthread_cond_t

pthread_cond_t

Identifies a condition variable.

pthread_condattr_t

pthread_condattr_t

Identifies a condition attribute.

pthread_key_t

pthread_key_t

Identifies a thread-specific data key.

pthread_mutex_t

pthread_mutex_t

Identifies a mutex.

pthread_mutexattr_t

pthread_mutexattr_t

Identifies a mutex attributes object.

pthread_once_t

pthread_once_t

Identifies a one-time initialization object.

struct sched_param {
int sched_priority; int sched_policy;
int sched_reserved; }

struct __sched_param { int __sched_priority; }

Used for setting thread attributes. AIX: Intended for developer use and contains three member variables. Linux: Not intended for developer use and contains one member variable.


Table 5-15. AIX and Linux POSIX Threads Comparison: APIs

AIX

Linux

Comment

pthread_atfork

pthread_atfork

Registers fork cleanup handlers.

pthread_attr_destroy

pthread_attr_destroy

Deletes a thread attribute.

pthread_attr_ getdetachstate pthread_attr_ setdetachstate

pthread_attr_ getdetachstate pthread_attr_ setdetachstate

Sets and returns the value of the detachstate attribute of a thread attributes object.

pthread_attr_ setstackaddr pthread_attr_ getstackaddr

pthread_attr_ setstackaddr pthread_attr_ getstackaddr

Returns and sets the value of the stackaddr attribute of a thread attributes object.

pthread_attr_init

pthread_attr_init

Creates a thread attributes object and initializes it with default values.

pthread_attr_ setstacksize pthread_attr_ getstacksize

pthread_attr_ setstacksize pthread_attr_ getstacksize

Returns and sets the value of the stacksize attribute of a thread attributes object.

pthread_cancel

pthread_cancel

Request cancellation of a pthread.

pthread_cleanup_pop pthread_cleanup_push

pthread_cleanup_pop pthread_cleanup_push

Activates and deactivates thread cancellation handlers.

pthread_detach

pthread_detach

Used to indicate to the implementation that storage for the thread whose thread ID is in the location thread can be reclaimed when that thread terminates.

pthread_equal

pthread_equal

Compares two thread IDs to determine if they refer to the same thread.

pthread_exit

pthread_exit

Terminates the calling thread and stores the status in the return value.

pthread_getspecific pthread_setspecific

pthread_getspecific pthread_setspecific

Returns and sets the thread-specific data associated with the specified key.

pthread_join

pthread_join

Blocks the calling thread until the specified thread terminates.

pthread_key_create pthread_key_delete

pthread_key_create pthread_key_delete

Creates and deletes a thread-specific data key. pthread_key_create also accepts a ptr to a function that will be called upon destruction.

pthread_kill

pthread_kill

The pthread_kill subroutine sends the signal signal to the thread thread. It acts with threads like the kill subroutine with single-threaded processes.

pthread_mutex_destroy pthread_mutex_init

pthread_mutex_destroy pthread_mutex_init

Initializes or destroys a mutex. Passing in NULL initializes all attributes to default values. Behavior is consistent between AIX and Linux.

pthread_mutexattr_init pthread_mutexattr_destroy

pthread_mutexattr_init pthread_mutexattr_destroy

Initializes and destroys mutex attributes.

pthread_mutexattr_ getpshared pthread_mutexattr_ setpshared

pthread_mutexattr_getpshared pthread_mutexattr_setpshared

Sets and gets process-shared attribute.

pthread_once

pthread_once

Executes a routine exactly once in a process.

pthread_self

pthread_self

Returns the indentifier of the current thread.


Table 5-16 shows APIs that were found to have differences between AIX and Linux and will require some coding changes when porting from AIX to Linux.

Table 5-16. AIX and Linux POSIX Threads Comparison: Nonportable

API

Differences

pthread_attr_init

Creates a new thread attribute with default values with the following exceptions. AIX: Contention scope = PTHREAD_SCOPE_PROCESS Sched param.sched_prio = 1 Linux: Contention scope = PTHREAD_SCOPE_SYSTEM Sched param.sched_priority = 0

pthread_mutex_lock pthread_mutex_trylock pthread_mutex_unlockj

Lock mechanism between AIX and Linux is not compatible. The conditions for deadlocking differ, and the attribute values differ. AIX: Possible attribute values PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_ERRORCHECKING PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_DEFAULT Linux: Possible attribute values #ifdef __USE_GNU
PTHREAD_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP

#ifdef __USE_UNIX98 PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_ERRORCHECKING PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_DEFAULT Refer to the Linux and AIX documentation on the different behavior conditions that could cause a deadlock.

pthread_mutexattr_gettype pthread_mutexattr_settype

Gets or sets a mutex type to the following values: AIX: Possible attribute values PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_ERRORCHECKING PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_DEFAULT
Linux: Possible attribute values #ifdef __USE_GNU PTHREAD_MUTEX_INITIALIZER PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP #ifdef __USE_UNIX98 PTHREAD_MUTEX_NORMAL PTHREAD_MUTEX_ERRORCHECKING PTHREAD_MUTEX_RECURSIVE PTHREAD_MUTEX_DEFAULT


NPTLcreated by Ulrich Drepperwas first released September 20, 2002. With the sponsorship of Red Hat and a collaborative effort among kernel and runtime developers, NPTL quickly replaced the old LinuxThreads library and became part of the GNU C Library distribution. It corrected some shortfalls on POSIX standards in the old LinuxThreads library and, with its thin layer, significantly increased performance. See Chapter 3 for more information about the Linux Native POSIX Threads Library.




UNIX to Linux Porting. A Comprehensive Reference
UNIX to Linux Porting: A Comprehensive Reference
ISBN: 0131871099
EAN: 2147483647
Year: 2004
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net