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