1 .\" Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH pthread_atfork 3 (date) "Linux man-pages (unreleased)"
7 pthread_atfork \- register fork handlers
10 .RI ( libpthread ", " \-lpthread )
13 .B #include <pthread.h>
15 .BI "int pthread_atfork(void (*" prepare ")(void), void (*" parent ")(void),"
16 .BI " void (*" child ")(void));"
21 function registers fork handlers that are to be executed when
23 is called by any thread in a process.
24 The handlers are executed in the context of the thread that calls
27 Three kinds of handler can be registered:
30 specifies a handler that is executed in the parent process before
35 specifies a handler that is executed in the parent process after
40 specifies a handler that is executed in the child process after
44 Any of the three arguments may be NULL if no handler is needed
45 in the corresponding phase of
52 On error, it returns an error number.
54 may be called multiple times by a process
55 to register additional handlers.
56 The handlers for each phase are called in a specified order: the
58 handlers are called in reverse order of registration; the
62 handlers are called in the order of registration.
66 Could not allocate memory to record the fork handler list entry.
68 POSIX.1-2001, POSIX.1-2008.
72 is called in a multithreaded process,
73 only the calling thread is duplicated in the child process.
74 The original intention of
76 was to allow the child process to be returned to a consistent state.
77 For example, at the time of the call to
79 other threads may have locked mutexes that are visible in the
80 user-space memory duplicated in the child.
81 Such mutexes would never be unlocked,
82 since the threads that placed the locks are not duplicated in the child.
85 was to provide a mechanism whereby the application (or a library)
86 could ensure that mutexes and other process and thread state would be
87 restored to a consistent state.
88 In practice, this task is generally too difficult to be practicable.
92 in a multithreaded process returns in the child,
93 the child should call only async-signal-safe functions (see
94 .BR signal\-safety (7))
95 until such time as it calls
97 to execute a new program.
99 POSIX.1 specifies that
100 .BR pthread_atfork ()
101 shall not fail with the error