Merge commit 'b31ca922c7346747131aed07c0c171ec2f573aac' into merges
[unleashed.git] / share / man / man3c / pthread_atfork.3c
blob3280ec5a8f07e0c67a3612384e181ba58e5bfc6f
1 '\" te
2 .\" Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
3 .\" Copyright (c) 1995 IEEE  All Rights Reserved.
4 .\" Copyright 1991, 1992, 1994, The X/Open Company Ltd.
5 .\" Portions Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
6 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
7 .\" http://www.opengroup.org/bookstore/.
8 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 .\"  This notice shall appear on any product containing this material.
10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
11 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
12 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH PTHREAD_ATFORK 3C "Dec 12, 2003"
14 .SH NAME
15 pthread_atfork \- register fork handlers
16 .SH SYNOPSIS
17 .LP
18 .nf
19 #include <sys/types.h>
20 #include <unistd.h>
22 \fBint\fR \fBpthread_atfork\fR(\fBvoid (*\fR\fIprepare\fR) (void), \fBvoid (*\fR\fIparent\fR) (void),
23      \fBvoid (*\fR\fIchild\fR) (void));
24 .fi
26 .SH DESCRIPTION
27 .sp
28 .LP
29 The \fBpthread_atfork()\fR function declares fork handlers to be called prior
30 to and following \fBfork\fR(2), within the thread that called \fBfork()\fR. The
31 order of calls to \fBpthread_atfork()\fR is significant.
32 .sp
33 .LP
34 Before \fBfork()\fR processing begins, the \fIprepare\fR fork handler is
35 called. The \fIprepare\fR handler is not called if its address is \fINULL.\fR
36 .sp
37 .LP
38 The \fIparent\fR fork handler is called after \fBfork()\fR processing finishes
39 in the parent process, and the \fIchild\fR fork handler is called after
40 \fBfork()\fR processing finishes in the child process. If the address of
41 \fIparent\fR or \fIchild\fR is  \fINULL\fR, then its handler is not called.
42 .sp
43 .LP
44 The \fIprepare\fR fork handler is called in  \fBLIFO\fR (last-in first-out)
45 order, whereas the \fIparent\fR and \fIchild\fR fork handlers are called in
46 \fBFIFO\fR (first-in first-out) order. This calling order allows applications
47 to preserve locking order.
48 .SH RETURN VALUES
49 .sp
50 .LP
51 Upon successful completion, \fBpthread_atfork()\fR returns \fB0\fR. Otherwise,
52 an error number is returned.
53 .SH ERRORS
54 .sp
55 .LP
56 The \fBpthread_atfork()\fR function will fail if:
57 .sp
58 .ne 2
59 .na
60 \fB\fBENOMEM\fR \fR
61 .ad
62 .RS 11n
63 Insufficient table space exists to record the fork handler addresses.
64 .RE
66 .SH USAGE
67 .sp
68 .LP
69 Solaris threads do not offer \fBpthread_atfork()\fR functionality (there is no
70 \fBthr_atfork()\fR interface). However, a Solaris threads application can call
71 \fBpthread_atfork()\fR to ensure \fBfork()\fR-safety, since the two thread APIs
72 are interoperable. See \fBfork\fR(2) for information relating to \fBfork()\fR in
73 a Solaris threads environment in Solaris 10 relative to previous releases.
74 .SH EXAMPLES
75 .LP
76 \fBExample 1 \fRMake a library safe with respect to \fBfork()\fR.
77 .sp
78 .LP
79 All multithreaded applications that call \fBfork()\fR in a POSIX threads
80 program and do more than simply call \fBexec\fR(2) in the child of the fork
81 need to ensure that the child is protected from deadlock.
83 .sp
84 .LP
85 Since the "fork-one" model results in duplicating only the thread that called
86 \fBfork()\fR, it is possible that at the time of the call another thread in the
87 parent owns a lock. This thread is not duplicated in the child, so no thread
88 will unlock this lock in the child.  Deadlock occurs if the single thread in
89 the child needs this lock.
91 .sp
92 .LP
93 The problem is more serious with locks in libraries.  Since a library writer
94 does not know if the application using the library calls \fBfork()\fR, the
95 library must protect itself from such a deadlock scenario.  If the application
96 that links with this library calls \fBfork()\fR and does not call  \fBexec()\fR
97 in the child, and if it needs a library lock that may be held by some other
98 thread in the parent that is inside the library at the time of the fork, the
99 application deadlocks inside the library.
103 The following describes how to make a library  safe with respect to
104 \fBfork()\fR by using  \fBpthread_atfork()\fR.
106 .RS +4
109 Identify all locks used by the library (for example \fB{L1,\|.\|.\|.Ln}\fR).
110 Identify also the locking order for these locks (for example
111 \fB{L1\|.\|.\|.Ln}, as well\fR.)
113 .RS +4
116 Add a call to \fBpthread_atfork(f1, f2, f3)\fR in the  library's
117 \fI\&.init\fR section.  \fBf1\fR, \fBf2\fR, \fBf3\fR are defined as follows:
120 .in +2
122 \fB        f1(\|)
123         {
124                 /* ordered in lock order */
125                 pthread_mutex_lock(L1);
126                 pthread_mutex_lock(\|.\|.\|.);
127                 pthread_mutex_lock(Ln);
128         }
130         f2(\|)
131         {
132                 pthread_mutex_unlock(L1);
133                 pthread_mutex_unlock(\|.\|.\|.);
134                 pthread_mutex_unlock(Ln);
135         }
137         f3(\|)
138         {
139                 pthread_mutex_unlock(L1);
140                 pthread_mutex_unlock(\|.\|.\|.);
141                 pthread_mutex_unlock(Ln);
142         }\fR
144 .in -2
146 .SH ATTRIBUTES
149 See \fBattributes\fR(5) for descriptions of the following attributes:
154 box;
155 c | c
156 l | l .
157 ATTRIBUTE TYPE  ATTRIBUTE VALUE
159 Interface Stability     Standard
161 MT-Level        MT-Safe
164 .SH SEE ALSO
167 \fBexec\fR(2), \fBfork\fR(2), \fBatexit\fR(3C), \fBattributes\fR(5),
168 \fBstandards\fR(5)