2 .\" Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_ATFORK 3 2017-09-15 "Linux" "Linux Programmer's Manual"
28 pthread_atfork \- register fork handlers
31 .B #include <pthread.h>
33 .BI "int pthread_atfork(void (*" prepare ")(void), void (*" parent ")(void),"
34 .BI " void (*" child ")(void));"
37 Link with \fI\-pthread\fP.
41 function registers fork handlers that are to be executed when
43 is called by this thread.
44 The handlers are executed in the context of the thread that calls
47 Three kinds of handler can be registered:
50 specifies a handler that is executed before
55 specifies a handler that is executed in the parent process after
60 specifies a handler that is executed in the child process after
64 Any of the three arguments may be NULL if no handler is needed
65 in the corresponding phase of
73 On error, it returns an error number.
75 may be called multiple times by a thread,
76 to register multiple handlers for each phase.
77 The handlers for each phase are called in a specified order: the
79 handlers are called in reverse order of registration; the
83 handlers are called in the order of registration.
87 Could not allocate memory to record the form handler entry.
89 POSIX.1-2001, POSIX.1-2008.
93 is called in a multithreaded process,
94 only the calling thread is duplicated in the child process.
95 The original intention of
97 was to allow the calling thread to be returned to a consistent state.
98 For example, at the time of the call to
100 other threads may have locked mutexes that are visible in the
101 user-space memory duplicated in the child.
102 Such mutexes would never be unlocked,
103 since the threads that placed the locks are not duplicated in the child.
105 .BR pthread_atfork ()
106 was to provide a mechanism whereby the application (or a library)
107 could ensure that mutexes and other process and thread state would be
108 restored to a consistent state.
109 In practice, this task is generally too difficult to be practicable.
113 in a multithreaded process returns in the child,
114 the child should call only async-signal-safe functions (see
115 .BR signal-safety (7))
116 until such time as it calls
118 to execute a new program.
120 POSIX.1 specifies that
121 .BR pthread_atfork ()
122 shall not fail with the error