]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man7/pthreads.7
Hyphen/dash fixes
[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.\"
21.TH PTHREADS 7 2005-06-07 "Linux 2.6.12" "Linux Programmer's Manual"
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
64.RB ( timer_create ())
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
85.RM ( pthread_sigmask ())
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
108.IR "cc -pthread" .
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
113.B LinuxThreads.
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.
23a6e651 130In NPTL, thread synchronisation 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
23a6e651
MK
140In addition to the main (initial) thread,
141and the threads that the program creates using
3616b7c0
MK
142.BR pthread_create (),
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.
194This can cause complications with set-UID programs and
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
23a6e651
MK
246Threads have distinct alternate signal stack settings.
247However, a new thread's alternate signal stack settings
248are copied from the the thread that created it, so that
249the threads initially share an alternate signal stack.
3616b7c0
MK
250.IP \- 3
251Threads do not share a common nice value.
252.IP \- 3
253Only the main thread is permitted to start a new session using
254.BR setsid (2).
255.\" FIXME why is there this limitation on setsid()?
256.IP \- 3
23a6e651 257Only the main thread is permitted to make the process into a
3616b7c0
MK
258process group leader using
259.BR setpgid (2).
260.\" FIXME why is there this limitation on setpgid()?
261.PP
262Some NPTL non-conformances only occur with older kernels:
263.IP \- 3
264The information returned by
265.BR times (2)
266and
267.BR getrusage (2)
268is per-thread rather than process-wide (fixed in kernel 2.6.9).
269.IP \- 3
270Threads do not share resource limits (fixed in kernel 2.6.10).
271.IP \- 3
272Threads do not share interval timers (fixed in kernel 2.6.12).
273.SS "Determining the Threading Implementation"
23a6e651 274Since glibc 2.3.2, the
3616b7c0
MK
275.BR getconf (1)
276command can be used to determine
277the system's default threading implementation, for example:
278.nf
279.in +4
280
281bash$ getconf GNU_LIBPTHREAD_VERSION
282NPTL 2.3.4
283.in -4
284.fi
285.PP
286With older glibc versions, a command such as the following should
287be sufficient to determine the default threading implementation:
288.nf
289.in +4
290
291bash$ $( ldd /bin/ls | grep libc.so | awk '{print $3}' ) | \\
292 egrep -i 'threads|ntpl'
293 Native POSIX Threads Library by Ulrich Drepper et al
294.in -4
295.fi
296.SS "Selecting the Threading Implementation: LD_ASSUME_KERNEL"
297On systems with a glibc that supports both LinuxThreads and NPTL,
298the LD_ASSUME_KERNEL environment variable can be used to override
299the dynamic linker's default choice of threading implementation.
23a6e651 300This variable tells the dynamic linker to assume that it is
3616b7c0
MK
301running on top of a particular kernel version.
302By specifying a kernel version that does not
303provide the support required by NPTL, we can force the use
304of LinuxThreads.
305(The most likely reason for doing this is to run a
306(broken) application that depends on some non-conformant behavior
307in LinuxThreads.)
308For example:
309.nf
310.in +4
311
312bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \\
313 awk '{print $3}' ) | egrep -i 'threads|ntpl'
314 linuxthreads-0.10 by Xavier Leroy
315.in -4
316.fi
317.SH "SEE ALSO"
318.BR clone (2),
319.BR futex (2),
320.BR gettid (2),
321.BR futex (4),
322and various Pthreads manual pages, for example:
323.BR pthread_atfork (3),
324.BR pthread_cleanup_push (3),
325.BR pthread_cond_signal (3),
326.BR pthread_cond_wait (3),
327.BR pthread_create (3),
328.BR pthread_detach (3),
329.BR pthread_equal (3),
330.BR pthread_exit (3),
331.BR pthread_key_create (3),
332.BR pthread_kill (3),
333.BR pthread_mutex_lock (3),
334.BR pthread_mutex_unlock (3),
335.BR pthread_once (3),
336.BR pthread_setcancelstate (3),
337.BR pthread_setcanceltype (3),
338.BR pthread_setspecific (3),
339.BR pthread_sigmask (3),
340and
341.BR pthread_testcancel (3).