'\" te .\" Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved. .\" Copyright (c) 1995 IEEE All Rights Reserved. .\" Copyright 1991, 1992, 1994, The X/Open Company Ltd. .\" Portions Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved. .\" 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 .\" http://www.opengroup.org/bookstore/. .\" 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. .\" This notice shall appear on any product containing this material. .\" 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. .\" 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. .\" 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] .TH pthread_atfork 3C "12 Dec 2003" "SunOS 5.11" "Standard C Library Functions" .SH NAME pthread_atfork \- register fork handlers .SH SYNOPSIS .LP .nf #include #include \fBint\fR \fBpthread_atfork\fR(\fBvoid (*\fR\fIprepare\fR) (void), \fBvoid (*\fR\fIparent\fR) (void), \fBvoid (*\fR\fIchild\fR) (void)); .fi .SH DESCRIPTION .sp .LP The \fBpthread_atfork()\fR function declares fork handlers to be called prior to and following \fBfork\fR(2), within the thread that called \fBfork()\fR. The order of calls to \fBpthread_atfork()\fR is significant. .sp .LP Before \fBfork()\fR processing begins, the \fIprepare\fR fork handler is called. The \fIprepare\fR handler is not called if its address is \fINULL.\fR .sp .LP The \fIparent\fR fork handler is called after \fBfork()\fR processing finishes in the parent process, and the \fIchild\fR fork handler is called after \fBfork()\fR processing finishes in the child process. If the address of \fIparent\fR or \fIchild\fR is \fINULL\fR, then its handler is not called. .sp .LP The \fIprepare\fR fork handler is called in \fBLIFO\fR (last-in first-out) order, whereas the \fIparent\fR and \fIchild\fR fork handlers are called in \fBFIFO\fR (first-in first-out) order. This calling order allows applications to preserve locking order. .SH RETURN VALUES .sp .LP Upon successful completion, \fBpthread_atfork()\fR returns \fB0\fR. Otherwise, an error number is returned. .SH ERRORS .sp .LP The \fBpthread_atfork()\fR function will fail if: .sp .ne 2 .mk .na \fB\fBENOMEM\fR \fR .ad .RS 11n .rt Insufficient table space exists to record the fork handler addresses. .RE .SH USAGE .sp .LP Solaris threads do not offer \fBpthread_atfork()\fR functionality (there is no \fBthr_atfork()\fR interface). However, a Solaris threads application can call \fBpthread_atfork()\fR to ensure \fBfork()\fR-safety, since the two thread APIs are interoperable. See\fBfork\fR(2) for information relating to \fBfork()\fR in a Solaris threads environment in Solaris 10 relative to previous releases. .SH EXAMPLES .LP \fBExample 1 \fRMake a library safe with respect to \fBfork()\fR. .sp .LP All multithreaded applications that call \fBfork()\fR in a POSIX threads program and do more than simply call \fBexec\fR(2) in the child of the fork need to ensure that the child is protected from deadlock. .sp .LP Since the "fork-one" model results in duplicating only the thread that called \fBfork()\fR, it is possible that at the time of the call another thread in the parent owns a lock. This thread is not duplicated in the child, so no thread will unlock this lock in the child. Deadlock occurs if the single thread in the child needs this lock. .sp .LP The problem is more serious with locks in libraries. Since a library writer does not know if the application using the library calls \fBfork()\fR, the library must protect itself from such a deadlock scenario. If the application that links with this library calls \fBfork()\fR and does not call \fBexec()\fR in the child, and if it needs a library lock that may be held by some other thread in the parent that is inside the library at the time of the fork, the application deadlocks inside the library. .sp .LP The following describes how to make a library safe with respect to \fBfork()\fR by using \fBpthread_atfork()\fR. .RS +4 .TP 1. Identify all locks used by the library (for example \fB{L1,\|.\|.\|.Ln}\fR). Identify also the locking order for these locks (for example \fB{L1\|.\|.\|.Ln}, as well\fR.) .RE .RS +4 .TP 2. Add a call to \fBpthread_atfork(f1, f2, f3)\fR in the library's \fI\&.init\fR section. \fBf1\fR, \fBf2\fR, \fBf3\fR are defined as follows: .RE .sp .in +2 .nf \fB f1(\|) { /* ordered in lock order */ pthread_mutex_lock(L1); pthread_mutex_lock(\|.\|.\|.); pthread_mutex_lock(Ln); } f2(\|) { pthread_mutex_unlock(L1); pthread_mutex_unlock(\|.\|.\|.); pthread_mutex_unlock(Ln); } f3(\|) { pthread_mutex_unlock(L1); pthread_mutex_unlock(\|.\|.\|.); pthread_mutex_unlock(Ln); }\fR .fi .in -2 .SH ATTRIBUTES .sp .LP See \fBattributes\fR(5) for descriptions of the following attributes: .sp .sp .TS tab() box; cw(2.75i) |cw(2.75i) lw(2.75i) |lw(2.75i) . ATTRIBUTE TYPEATTRIBUTE VALUE _ Interface StabilityStandard _ MT-LevelMT-Safe .TE .SH SEE ALSO .sp .LP \fBexec\fR(2), \fBfork\fR(2), \fBatexit\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)