Debian Codegraph @

upstream/fedora-44/man3/pthread_atfork.3

raw
1 .\" Copyright (C) 2017 Michael Kerrisk <[email protected]>
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH pthread_atfork 3 2024-12-13 "Linux man-pages 6.13"
6 .SH NAME
7 pthread_atfork \- register fork handlers
8 .SH LIBRARY
9 POSIX threads library
10 .RI ( libpthread ,\~ \-lpthread )
11 .SH SYNOPSIS
12 .nf
13 .B #include <pthread.h>
14 .P
15 .BI "int pthread_atfork(typeof(void (void)) *" prepare ,
16 .BI " typeof(void (void)) *" parent ,
17 .BI " typeof(void (void)) *" child );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR pthread_atfork ()
22 function registers fork handlers that are to be executed when
23 .BR fork (2)
24 is called by any thread in a process.
25 The handlers are executed in the context of the thread that calls
26 .BR fork (2).
27 .P
28 Three kinds of handler can be registered:
29 .IP \[bu] 3
30 .I prepare
31 specifies a handler that is executed in the parent process before
32 .BR fork (2)
33 processing starts.
34 .IP \[bu]
35 .I parent
36 specifies a handler that is executed in the parent process after
37 .BR fork (2)
38 processing completes.
39 .IP \[bu]
40 .I child
41 specifies a handler that is executed in the child process after
42 .BR fork (2)
43 processing completes.
44 .P
45 Any of the three arguments may be NULL if no handler is needed
46 in the corresponding phase of
47 .BR fork (2)
48 processing.
49 .SH RETURN VALUE
50 On success,
51 .BR pthread_atfork ()
52 returns zero.
53 On error, it returns an error number.
54 .BR pthread_atfork ()
55 may be called multiple times by a process
56 to register additional handlers.
57 The handlers for each phase are called in a specified order: the
58 .I prepare
59 handlers are called in reverse order of registration; the
60 .I parent
61 and
62 .I child
63 handlers are called in the order of registration.
64 .SH ERRORS
65 .TP
66 .B ENOMEM
67 Could not allocate memory to record the fork handler list entry.
68 .SH STANDARDS
69 POSIX.1-2008.
70 .SH HISTORY
71 POSIX.1-2001.
72 .SH NOTES
73 When
74 .BR fork (2)
75 is called in a multithreaded process,
76 only the calling thread is duplicated in the child process.
77 The original intention of
78 .BR pthread_atfork ()
79 was to allow the child process to be returned to a consistent state.
80 For example, at the time of the call to
81 .BR fork (2),
82 other threads may have locked mutexes that are visible in the
83 user-space memory duplicated in the child.
84 Such mutexes would never be unlocked,
85 since the threads that placed the locks are not duplicated in the child.
86 The intent of
87 .BR pthread_atfork ()
88 was to provide a mechanism whereby the application (or a library)
89 could ensure that mutexes and other process and thread state would be
90 restored to a consistent state.
91 In practice, this task is generally too difficult to be practicable.
92 .P
93 After a
94 .BR fork (2)
95 in a multithreaded process returns in the child,
96 the child should call only async-signal-safe functions (see
97 .BR signal\-safety (7))
98 until such time as it calls
99 .BR execve (2)
100 to execute a new program.
101 .P
102 POSIX.1 specifies that
103 .BR pthread_atfork ()
104 shall not fail with the error
105 .BR EINTR .
106 .SH SEE ALSO
107 .BR fork (2),
108 .BR atexit (3),
109 .BR pthreads (7)