man-pages/man7/pthreads.7

478 lines
13 KiB
Groff
Raw Normal View History

2005-06-07 12:35:32 +00:00
'\" t
2007-09-20 06:52:22 +00:00
.\" Copyright (c) 2005 by Michael Kerrisk <mtk.manpages@gmail.com>
2005-06-07 12:35:32 +00:00
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
2005-06-07 12:35:32 +00:00
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
2007-10-23 14:33:53 +00:00
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
2005-06-07 12:35:32 +00:00
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
2008-08-24 05:46:38 +00:00
.TH PTHREADS 7 2008-08-24 "Linux" "Linux Programmer's Manual"
2005-06-07 12:35:32 +00:00
.SH NAME
pthreads \- POSIX threads
.SH DESCRIPTION
POSIX.1 specifies a set of interfaces (functions, header files) for
2005-06-07 12:35:32 +00:00
threaded programming commonly known as POSIX threads, or Pthreads.
A single process can contain multiple threads,
2005-06-07 12:35:32 +00:00
all of which are executing the same program.
These threads share the same global memory (data and heap segments),
but each thread has its own stack (automatic variables).
POSIX.1 also requires that threads share a range of other attributes
(i.e., these attributes are process-wide rather than per-thread):
.IP \- 3
process ID
.IP \- 3
parent process ID
.IP \- 3
process group ID and session ID
.IP \- 3
controlling terminal
.IP \- 3
user and group IDs
.IP \- 3
open file descriptors
.IP \- 3
record locks (see
.BR fcntl (2))
.IP \- 3
signal dispositions
.IP \- 3
file mode creation mask
.RB ( umask (2))
.IP \- 3
current directory
2005-06-07 12:35:32 +00:00
.RB ( chdir (2))
and
root directory
.RB ( chroot (2))
.IP \- 3
interval timers
.RB ( setitimer (2))
and POSIX timers
2007-05-11 23:29:44 +00:00
.RB ( timer_create (3))
2005-06-07 12:35:32 +00:00
.IP \- 3
nice value
.RB ( setpriority (2))
.IP \- 3
resource limits
.RB ( setrlimit (2))
.IP \- 3
measurements of the consumption of CPU time
2005-06-07 12:35:32 +00:00
.RB ( times (2))
and resources
2005-06-07 12:35:32 +00:00
.RB ( getrusage (2))
.PP
As well as the stack, POSIX.1 specifies that various other
attributes are distinct for each thread, including:
.IP \- 3
thread ID (the
.I pthread_t
data type)
.IP \- 3
signal mask
2007-05-11 23:29:44 +00:00
.RB ( pthread_sigmask (3))
2005-06-07 12:35:32 +00:00
.IP \- 3
the
.I errno
variable
.IP \- 3
alternate signal stack
2005-06-07 12:35:32 +00:00
.RB ( sigaltstack (2))
.IP \- 3
real-time scheduling policy and priority
.RB ( sched_setscheduler (2)
and
.BR sched_setparam (2))
.PP
The following Linux-specific features are also per-thread:
.IP \- 3
capabilities (see
.BR capabilities (7))
.IP \- 3
CPU affinity
2005-06-07 12:35:32 +00:00
.RB ( sched_setaffinity (2))
2008-05-12 22:20:30 +00:00
.SS "Thread-safe functions"
A thread-safe function is one that can be safely
(i.e., it will deliver the same results regardless of whether it is)
called from multiple threads at the same time.
POSIX.1-2001 requires that all functions specified in the standard
shall be thread-safe, except for the following functions:
.in +4n
.nf
asctime()
basename()
catgets()
crypt()
ctermid() if passed a non-NULL argument
ctime()
dbm_clearerr()
dbm_close()
dbm_delete()
dbm_error()
dbm_fetch()
dbm_firstkey()
dbm_nextkey()
dbm_open()
dbm_store()
dirname()
dlerror()
drand48()
ecvt()
encrypt()
endgrent()
endpwent()
endutxent()
fcvt()
ftw()
gcvt()
getc_unlocked()
getchar_unlocked()
getdate()
getenv()
getgrent()
getgrgid()
getgrnam()
gethostbyaddr()
gethostbyname()
gethostent()
getlogin()
getnetbyaddr()
getnetbyname()
getnetent()
getopt()
getprotobyname()
getprotobynumber()
getprotoent()
getpwent()
getpwnam()
getpwuid()
getservbyname()
getservbyport()
getservent()
getutxent()
getutxid()
getutxline()
gmtime()
hcreate()
hdestroy()
hsearch()
inet_ntoa()
l64a()
lgamma()
lgammaf()
lgammal()
localeconv()
localtime()
lrand48()
mrand48()
nftw()
nl_langinfo()
ptsname()
putc_unlocked()
putchar_unlocked()
putenv()
pututxline()
rand()
readdir()
setenv()
setgrent()
setkey()
setpwent()
setutxent()
strerror()
strtok()
tmpnam() if passed a non-NULL argument
ttyname()
unsetenv()
wcrtomb() if its final argument is NULL
wcsrtombs() if its final argument is NULL
wcstombs()
wctomb()
.fi
.in
.PP
POSIX.1-2008 removes ecvt(), fcvt(), gcvt(), gethostbyname(),
and gethostbyaddr() from the above list
(because those functions are removed from the standard), and adds
strerror() and system().
2005-06-07 12:35:32 +00:00
.SS "Compiling on Linux"
On Linux, programs that use the Pthreads API should be compiled using
2005-07-06 07:41:37 +00:00
.IR "cc \-pthread" .
2005-06-07 12:35:32 +00:00
.SS "Linux Implementations of POSIX Threads"
Over time, two threading implementations have been provided by
the GNU C library on Linux:
2008-01-08 13:22:09 +00:00
.TP
2005-07-19 15:36:19 +00:00
.B LinuxThreads
This is the original Pthreads implementation.
Since glibc 2.4, this implementation is no longer supported.
2008-01-08 13:22:09 +00:00
.TP
.BR NPTL " (Native POSIX Threads Library)"
2005-06-07 12:35:32 +00:00
This is the modern Pthreads implementation.
By comparison with LinuxThreads, NPTL provides closer conformance to
the requirements of the POSIX.1 specification and better performance
2005-06-07 12:35:32 +00:00
when creating large numbers of threads.
NPTL is available since glibc 2.3.2,
and requires features that are present in the Linux 2.6 kernel.
2005-06-07 12:35:32 +00:00
.PP
Both of these are so-called 1:1 implementations, meaning that each
thread maps to a kernel scheduling entity.
Both threading implementations employ the Linux
.BR clone (2)
system call.
In NPTL, thread synchronization primitives (mutexes,
2005-06-07 12:35:32 +00:00
thread joining, etc.) are implemented using the Linux
.BR futex (2)
system call.
.SS LinuxThreads
The notable features of this implementation are the following:
.IP \- 3
In addition to the main (initial) thread,
and the threads that the program creates using
2007-05-11 23:29:44 +00:00
.BR pthread_create (3),
2005-06-07 12:35:32 +00:00
the implementation creates a "manager" thread.
This thread handles thread creation and termination.
(Problems can result if this thread is inadvertently killed.)
.IP \- 3
Signals are used internally by the implementation.
On Linux 2.2 and later, the first three real-time signals are used.
2007-06-21 05:38:48 +00:00
On older Linux kernels,
.B SIGUSR1
and
.B SIGUSR2
are used.
Applications must avoid the use of whichever set of signals is
2005-06-07 12:35:32 +00:00
employed by the implementation.
.IP \- 3
Threads do not share process IDs.
(In effect, LinuxThreads threads are implemented as processes which share
more information than usual, but which do not share a common process ID.)
LinuxThreads threads (including the manager thread)
are visible as separate processes using
.BR ps (1).
.PP
The LinuxThreads implementation deviates from the POSIX.1
specification in a number of ways, including the following:
.IP \- 3
Calls to
.BR getpid (2)
return a different value in each thread.
.IP \- 3
Calls to
2005-06-07 12:35:32 +00:00
.BR getppid (2)
in threads other than the main thread return the process ID of the
manager thread; instead
.BR getppid (2)
in these threads should return the same value as
.BR getppid (2)
in the main thread.
.IP \- 3
When one thread creates a new child process using
.BR fork (2),
any thread should be able to
.BR wait (2)
on the child.
However, the implementation only allows the thread that
2005-06-07 12:35:32 +00:00
created the child to
.BR wait (2)
on it.
.IP \- 3
When a thread calls
.BR execve (2),
all other threads are terminated (as required by POSIX.1).
However, the resulting process has the same PID as the thread that called
.BR execve (2):
it should have the same PID as the main thread.
.IP \- 3
Threads do not share user and group IDs.
This can cause complications with set-user-ID programs and
2005-06-07 12:35:32 +00:00
can cause failures in Pthreads functions if an application
changes its credentials using
.BR seteuid (2)
or similar.
.IP \- 3
Threads do not share a common session ID and process group ID.
.IP \- 3
Threads do not share record locks created using
.BR fcntl (2).
.IP \- 3
The information returned by
.BR times (2)
and
.BR getrusage (2)
is per-thread rather than process-wide.
.IP \- 3
Threads do not share semaphore undo values (see
.BR semop (2)).
.IP \- 3
Threads do not share interval timers.
.IP \- 3
Threads do not share a common nice value.
.IP \- 3
POSIX.1 distinguishes the notions of signals that are directed
2008-03-25 06:29:23 +00:00
to the process as a whole and signals that are directed to individual
2005-06-07 12:35:32 +00:00
threads.
According to POSIX.1, a process-directed signal (sent using
.BR kill (2),
for example) should be handled by a single,
arbitrarily selected thread within the process.
LinuxThreads does not support the notion of process-directed signals:
signals may only be sent to specific threads.
.IP \- 3
Threads have distinct alternate signal stack settings.
However, a new thread's alternate signal stack settings
2005-07-05 13:53:03 +00:00
are copied from the thread that created it, so that
the threads initially share an alternate signal stack.
(A new thread should start with no alternate signal stack defined.
If two threads handle signals on their shared alternate signal
stack at the same time, unpredictable program failures are
likely to occur.)
2005-06-07 12:35:32 +00:00
.SS NPTL
With NPTL, all of the threads in a process are placed
in the same thread group;
all members of a thread groups share the same PID.
NPTL does not employ a manager thread.
NPTL makes internal use of the first two real-time signals;
these signals cannot be used in applications.
2008-01-08 13:22:09 +00:00
NPTL still has at least one non-conformance with POSIX.1:
2005-06-07 12:35:32 +00:00
.IP \- 3
Threads do not share a common nice value.
2006-03-22 20:18:10 +00:00
.\" FIXME . bug report filed for NPTL nice non-conformance
.\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
2008-09-09 10:48:13 +00:00
.\" Sep 08: there is a patch by Denys Vlasenko to address this
.\" "make setpriority POSIX compliant; introduce PRIO_THREAD extension"
.\" Monitor this to see if it makes it into mainline.
2005-06-07 12:35:32 +00:00
.PP
Some NPTL non-conformances only occur with older kernels:
.IP \- 3
The information returned by
.BR times (2)
and
.BR getrusage (2)
is per-thread rather than process-wide (fixed in kernel 2.6.9).
.IP \- 3
Threads do not share resource limits (fixed in kernel 2.6.10).
.IP \- 3
Threads do not share interval timers (fixed in kernel 2.6.12).
.IP \- 3
Only the main thread is permitted to start a new session using
.BR setsid (2)
(fixed in kernel 2.6.16).
.IP \- 3
Only the main thread is permitted to make the process into a
process group leader using
.BR setpgid (2)
(fixed in kernel 2.6.16).
.IP \- 3
Threads have distinct alternate signal stack settings.
However, a new thread's alternate signal stack settings
are copied from the thread that created it, so that
the threads initially share an alternate signal stack
(fixed in kernel 2.6.16).
.PP
Note the following further points about the NPTL implementation:
.IP \- 3
If the stack size soft resource limit (see the description of
.B RLIMIT_STACK
in
.BR setrlimit (2))
is set to a value other than
.IR unlimited ,
then this value defines the default stack size for new threads.
To be effective, this limit must be set before the program
is executed, perhaps using the
.I ulimit -s
shell built-in command
.RI ( "limit stacksize"
in the C shell).
2005-06-07 12:35:32 +00:00
.SS "Determining the Threading Implementation"
Since glibc 2.3.2, the
2005-06-07 12:35:32 +00:00
.BR getconf (1)
command can be used to determine
the system's threading implementation, for example:
2005-06-07 12:35:32 +00:00
.nf
.in +4n
2005-06-07 12:35:32 +00:00
bash$ getconf GNU_LIBPTHREAD_VERSION
NPTL 2.3.4
.in
2005-06-07 12:35:32 +00:00
.fi
.PP
With older glibc versions, a command such as the following should
be sufficient to determine the default threading implementation:
.nf
.in +4n
2005-06-07 12:35:32 +00:00
2008-06-09 15:49:35 +00:00
bash$ $( ldd /bin/ls | grep libc.so | awk \(aq{print $3}\(aq ) | \\
egrep \-i \(aqthreads|nptl\(aq
2005-06-07 12:35:32 +00:00
Native POSIX Threads Library by Ulrich Drepper et al
.in
2005-06-07 12:35:32 +00:00
.fi
.SS "Selecting the Threading Implementation: LD_ASSUME_KERNEL"
On systems with a glibc that supports both LinuxThreads and NPTL
(i.e., glibc 2.3.\fIx\fP), the
2007-06-22 20:40:07 +00:00
.B LD_ASSUME_KERNEL
environment variable can be used to override
2005-06-07 12:35:32 +00:00
the dynamic linker's default choice of threading implementation.
This variable tells the dynamic linker to assume that it is
2005-06-07 12:35:32 +00:00
running on top of a particular kernel version.
By specifying a kernel version that does not
provide the support required by NPTL, we can force the use
of LinuxThreads.
(The most likely reason for doing this is to run a
(broken) application that depends on some non-conformant behavior
in LinuxThreads.)
For example:
.nf
.in +4n
2005-06-07 12:35:32 +00:00
bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \\
2008-06-09 15:49:35 +00:00
awk \(aq{print $3}\(aq ) | egrep \-i \(aqthreads|ntpl\(aq
2005-06-07 12:35:32 +00:00
linuxthreads-0.10 by Xavier Leroy
.in
2005-06-07 12:35:32 +00:00
.fi
.SH "SEE ALSO"
.BR clone (2),
.BR futex (2),
.BR gettid (2),
2008-08-24 05:46:38 +00:00
.BR proc (5),
2006-04-21 01:24:06 +00:00
.BR futex (7),
.br
2005-06-07 12:35:32 +00:00
and various Pthreads manual pages, for example:
.BR pthread_atfork (3),
.BR pthread_cleanup_push (3),
.BR pthread_cond_signal (3),
.BR pthread_cond_wait (3),
.BR pthread_create (3),
.BR pthread_detach (3),
.BR pthread_equal (3),
.BR pthread_exit (3),
.BR pthread_key_create (3),
.BR pthread_kill (3),
.BR pthread_mutex_lock (3),
.BR pthread_mutex_unlock (3),
.BR pthread_once (3),
.BR pthread_setcancelstate (3),
.BR pthread_setcanceltype (3),
.BR pthread_setspecific (3),
.BR pthread_sigmask (3),
and
.BR pthread_testcancel (3)