]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/pthreads.7
Convert to American spelling conventions
[thirdparty/man-pages.git] / man7 / pthreads.7
CommitLineData
3616b7c0
MK
1'\" t
2.\" Copyright (c) 2005 by Michael Kerrisk <mtk-manpages@gmx.net>
3.\"
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
23a6e651 12.\"
3616b7c0
MK
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein.
23a6e651 17.\"
3616b7c0
MK
18.\" Formatted or processed versions of this manual, if unaccompanied by
19.\" the source, must acknowledge the copyright and authors of this work.
20.\"
d9343c5c 21.TH PTHREADS 7 2005-06-07 "Linux" "Linux Programmer's Manual"
3616b7c0
MK
22.SH NAME
23pthreads \- POSIX threads
24.SH DESCRIPTION
23a6e651 25POSIX.1 specifies a set of interfaces (functions, header files) for
3616b7c0 26threaded programming commonly known as POSIX threads, or Pthreads.
23a6e651 27A single process can contain multiple threads,
3616b7c0
MK
28all of which are executing the same program.
29These threads share the same global memory (data and heap segments),
30but each thread has its own stack (automatic variables).
31
32POSIX.1 also requires that threads share a range of other attributes
33(i.e., these attributes are process-wide rather than per-thread):
34.IP \- 3
35process ID
36.IP \- 3
37parent process ID
38.IP \- 3
39process group ID and session ID
40.IP \- 3
41controlling terminal
42.IP \- 3
43user and group IDs
44.IP \- 3
45open file descriptors
46.IP \- 3
47record locks (see
48.BR fcntl (2))
49.IP \- 3
50signal dispositions
51.IP \- 3
52file mode creation mask
53.RB ( umask (2))
54.IP \- 3
23a6e651 55current directory
3616b7c0
MK
56.RB ( chdir (2))
57and
58root directory
59.RB ( chroot (2))
60.IP \- 3
61interval timers
62.RB ( setitimer (2))
63and POSIX timers
63f6a20a 64.RB ( timer_create (3))
3616b7c0
MK
65.IP \- 3
66nice value
67.RB ( setpriority (2))
68.IP \- 3
69resource limits
70.RB ( setrlimit (2))
71.IP \- 3
23a6e651 72measurements of the consumption of CPU time
3616b7c0 73.RB ( times (2))
23a6e651 74and resources
3616b7c0
MK
75.RB ( getrusage (2))
76.PP
77As well as the stack, POSIX.1 specifies that various other
78attributes are distinct for each thread, including:
79.IP \- 3
80thread ID (the
81.I pthread_t
82data type)
83.IP \- 3
84signal mask
63f6a20a 85.RB ( pthread_sigmask (3))
3616b7c0
MK
86.IP \- 3
87the
88.I errno
89variable
90.IP \- 3
23a6e651 91alternate signal stack
3616b7c0
MK
92.RB ( sigaltstack (2))
93.IP \- 3
94real-time scheduling policy and priority
95.RB ( sched_setscheduler (2)
96and
97.BR sched_setparam (2))
98.PP
99The following Linux-specific features are also per-thread:
100.IP \- 3
101capabilities (see
102.BR capabilities (7))
103.IP \- 3
23a6e651 104CPU affinity
3616b7c0
MK
105.RB ( sched_setaffinity (2))
106.SS "Compiling on Linux"
107On Linux, programs that use the Pthreads API should be compiled using
4d9b6984 108.IR "cc \-pthread" .
3616b7c0
MK
109.SS "Linux Implementations of POSIX Threads"
110Over time, two threading implementations have been provided by
111the GNU C library on Linux:
112.IP \- 3
a5e0a0e4 113.B LinuxThreads
3616b7c0
MK
114This is the original (now obsolete) Pthreads implementation.
115.IP \- 3
23a6e651 116.B NPTL
3616b7c0
MK
117(Native POSIX Threads Library)
118This is the modern Pthreads implementation.
119By comparison with LinuxThreads, NPTL provides closer conformance to
23a6e651 120the requirements of the POSIX.1 specification and better performance
3616b7c0 121when creating large numbers of threads.
fd064f40 122NPTL requires features that are present in the Linux 2.6 kernel.
3616b7c0
MK
123.PP
124Both of these are so-called 1:1 implementations, meaning that each
125thread maps to a kernel scheduling entity.
126
127Both threading implementations employ the Linux
128.BR clone (2)
129system call.
d9bfdb9c 130In NPTL, thread synchronization primitives (mutexes,
3616b7c0
MK
131thread joining, etc.) are implemented using the Linux
132.BR futex (2)
133system call.
134.PP
135Modern GNU C libraries provide both LinuxThreads and NPTL, with the
136latter being the default (if supported by the underlying kernel).
137.SS LinuxThreads
138The notable features of this implementation are the following:
139.IP \- 3
c13182ef 140In addition to the main (initial) thread,
23a6e651 141and the threads that the program creates using
63f6a20a 142.BR pthread_create (3),
3616b7c0
MK
143the implementation creates a "manager" thread.
144This thread handles thread creation and termination.
145(Problems can result if this thread is inadvertently killed.)
146.IP \- 3
147Signals are used internally by the implementation.
148On Linux 2.2 and later, the first three real-time signals are used.
149On older Linux kernels, SIGUSR1 and SIGUSR2 are used.
23a6e651 150Applications must avoid the use of whichever set of signals is
3616b7c0
MK
151employed by the implementation.
152.IP \- 3
153Threads do not share process IDs.
154(In effect, LinuxThreads threads are implemented as processes which share
155more information than usual, but which do not share a common process ID.)
156LinuxThreads threads (including the manager thread)
157are visible as separate processes using
158.BR ps (1).
159.PP
160The LinuxThreads implementation deviates from the POSIX.1
161specification in a number of ways, including the following:
162.IP \- 3
163Calls to
164.BR getpid (2)
165return a different value in each thread.
166.IP \- 3
23a6e651 167Calls to
3616b7c0
MK
168.BR getppid (2)
169in threads other than the main thread return the process ID of the
170manager thread; instead
171.BR getppid (2)
172in these threads should return the same value as
173.BR getppid (2)
174in the main thread.
175.IP \- 3
176When one thread creates a new child process using
177.BR fork (2),
178any thread should be able to
179.BR wait (2)
180on the child.
23a6e651 181However, the implementation only allows the thread that
3616b7c0
MK
182created the child to
183.BR wait (2)
184on it.
185.IP \- 3
186When a thread calls
187.BR execve (2),
188all other threads are terminated (as required by POSIX.1).
189However, the resulting process has the same PID as the thread that called
190.BR execve (2):
191it should have the same PID as the main thread.
192.IP \- 3
193Threads do not share user and group IDs.
880f5b4b 194This can cause complications with set-user-ID programs and
3616b7c0
MK
195can cause failures in Pthreads functions if an application
196changes its credentials using
197.BR seteuid (2)
198or similar.
199.IP \- 3
200Threads do not share a common session ID and process group ID.
201.IP \- 3
202Threads do not share record locks created using
203.BR fcntl (2).
204.IP \- 3
205The information returned by
206.BR times (2)
207and
208.BR getrusage (2)
209is per-thread rather than process-wide.
210.IP \- 3
211Threads do not share semaphore undo values (see
212.BR semop (2)).
213.IP \- 3
214Threads do not share interval timers.
215.IP \- 3
216Threads do not share a common nice value.
217.IP \- 3
218POSIX.1 distinguishes the notions of signals that are directed
219to the process as a whole and signals are directed to individual
220threads.
221According to POSIX.1, a process-directed signal (sent using
222.BR kill (2),
223for example) should be handled by a single,
224arbitrarily selected thread within the process.
225LinuxThreads does not support the notion of process-directed signals:
226signals may only be sent to specific threads.
227.IP \- 3
23a6e651
MK
228Threads have distinct alternate signal stack settings.
229However, a new thread's alternate signal stack settings
61f4934a 230are copied from the thread that created it, so that
23a6e651
MK
231the threads initially share an alternate signal stack.
232(A new thread should start with no alternate signal stack defined.
233If two threads handle signals on their shared alternate signal
234stack at the same time, unpredictable program failures are
235likely to occur.)
3616b7c0
MK
236.SS NPTL
237With NPTL, all of the threads in a process are placed
238in the same thread group;
239all members of a thread groups share the same PID.
23a6e651
MK
240NPTL does not employ a manager thread.
241NPTL makes internal use of the first two real-time signals;
242these signals cannot be used in applications.
243
3616b7c0
MK
244NPTL still has a few non-conformances with POSIX.1:
245.IP \- 3
3616b7c0 246Threads do not share a common nice value.
651946d5
MK
247.\" FIXME . bug report filed for NPTL nice non-conformance
248.\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
3616b7c0
MK
249.PP
250Some NPTL non-conformances only occur with older kernels:
251.IP \- 3
252The information returned by
253.BR times (2)
254and
255.BR getrusage (2)
256is per-thread rather than process-wide (fixed in kernel 2.6.9).
257.IP \- 3
258Threads do not share resource limits (fixed in kernel 2.6.10).
259.IP \- 3
260Threads do not share interval timers (fixed in kernel 2.6.12).
2e174648
MK
261.IP \- 3
262Only the main thread is permitted to start a new session using
263.BR setsid (2)
264(fixed in kernel 2.6.16).
265.IP \- 3
266Only the main thread is permitted to make the process into a
267process group leader using
c13182ef 268.BR setpgid (2)
2e174648 269(fixed in kernel 2.6.16).
456ed12f
MK
270.IP \- 3
271Threads have distinct alternate signal stack settings.
272However, a new thread's alternate signal stack settings
273are copied from the thread that created it, so that
274the threads initially share an alternate signal stack
275(fixed in kernel 2.6.16).
5e8b726f
MK
276.PP
277Note the following further points about the NPTL implementation:
278.IP \- 3
279If the stack size soft resource limit (see the description of
c13182ef 280.B RLIMIT_STACK
5e8b726f
MK
281in
282.BR setrlimit (2))
283is set to a value other than
284.IR unlimited ,
285then this value defines the default stack size for new threads.
286To be effective, this limit must be set before the program
287is executed, perhaps using the
288.I ulimit -s
c13182ef 289shell built-in command
5e8b726f
MK
290.RI ( "limit stacksize"
291in the C shell).
3616b7c0 292.SS "Determining the Threading Implementation"
23a6e651 293Since glibc 2.3.2, the
3616b7c0
MK
294.BR getconf (1)
295command can be used to determine
296the system's default threading implementation, for example:
297.nf
298.in +4
299
300bash$ getconf GNU_LIBPTHREAD_VERSION
301NPTL 2.3.4
302.in -4
303.fi
304.PP
305With older glibc versions, a command such as the following should
306be sufficient to determine the default threading implementation:
307.nf
308.in +4
309
310bash$ $( ldd /bin/ls | grep libc.so | awk '{print $3}' ) | \\
2bc2f479 311 egrep \-i 'threads|ntpl'
3616b7c0
MK
312 Native POSIX Threads Library by Ulrich Drepper et al
313.in -4
314.fi
315.SS "Selecting the Threading Implementation: LD_ASSUME_KERNEL"
316On systems with a glibc that supports both LinuxThreads and NPTL,
317the LD_ASSUME_KERNEL environment variable can be used to override
318the dynamic linker's default choice of threading implementation.
23a6e651 319This variable tells the dynamic linker to assume that it is
3616b7c0
MK
320running on top of a particular kernel version.
321By specifying a kernel version that does not
322provide the support required by NPTL, we can force the use
323of LinuxThreads.
324(The most likely reason for doing this is to run a
325(broken) application that depends on some non-conformant behavior
326in LinuxThreads.)
327For example:
328.nf
329.in +4
330
331bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \\
2bc2f479 332 awk '{print $3}' ) | egrep \-i 'threads|ntpl'
3616b7c0
MK
333 linuxthreads-0.10 by Xavier Leroy
334.in -4
335.fi
336.SH "SEE ALSO"
337.BR clone (2),
338.BR futex (2),
339.BR gettid (2),
a8bda636 340.BR futex (7),
3616b7c0
MK
341and various Pthreads manual pages, for example:
342.BR pthread_atfork (3),
343.BR pthread_cleanup_push (3),
344.BR pthread_cond_signal (3),
345.BR pthread_cond_wait (3),
346.BR pthread_create (3),
347.BR pthread_detach (3),
348.BR pthread_equal (3),
349.BR pthread_exit (3),
350.BR pthread_key_create (3),
351.BR pthread_kill (3),
352.BR pthread_mutex_lock (3),
353.BR pthread_mutex_unlock (3),
354.BR pthread_once (3),
355.BR pthread_setcancelstate (3),
356.BR pthread_setcanceltype (3),
357.BR pthread_setspecific (3),
358.BR pthread_sigmask (3),
359and
360.BR pthread_testcancel (3).