1 .\" Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH PTHREAD_ATFORK 3 2020-08-13 "Linux" "Linux Programmer's Manual"
27 pthread_atfork \- register fork handlers
30 .B #include <pthread.h>
32 .BI "int pthread_atfork(void (*" prepare ")(void), void (*" parent ")(void),"
33 .BI " void (*" child ")(void));"
36 Link with \fI\-pthread\fP.
40 function registers fork handlers that are to be executed when
42 is called by this thread.
43 The handlers are executed in the context of the thread that calls
46 Three kinds of handler can be registered:
49 specifies a handler that is executed before
54 specifies a handler that is executed in the parent process after
59 specifies a handler that is executed in the child process after
63 Any of the three arguments may be NULL if no handler is needed
64 in the corresponding phase of
71 On error, it returns an error number.
73 may be called multiple times by a thread,
74 to register multiple handlers for each phase.
75 The handlers for each phase are called in a specified order: the
77 handlers are called in reverse order of registration; the
81 handlers are called in the order of registration.
85 Could not allocate memory to record the form handler entry.
87 POSIX.1-2001, POSIX.1-2008.
91 is called in a multithreaded process,
92 only the calling thread is duplicated in the child process.
93 The original intention of
95 was to allow the calling thread to be returned to a consistent state.
96 For example, at the time of the call to
98 other threads may have locked mutexes that are visible in the
99 user-space memory duplicated in the child.
100 Such mutexes would never be unlocked,
101 since the threads that placed the locks are not duplicated in the child.
103 .BR pthread_atfork ()
104 was to provide a mechanism whereby the application (or a library)
105 could ensure that mutexes and other process and thread state would be
106 restored to a consistent state.
107 In practice, this task is generally too difficult to be practicable.
111 in a multithreaded process returns in the child,
112 the child should call only async-signal-safe functions (see
113 .BR signal\-safety (7))
114 until such time as it calls
116 to execute a new program.
118 POSIX.1 specifies that
119 .BR pthread_atfork ()
120 shall not fail with the error