]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_atfork.3
fuse.4: ffix
[thirdparty/man-pages.git] / man3 / pthread_atfork.3
CommitLineData
aadc226b
MK
1'\" t
2.\" Copyright (C) 2017 Michael Kerrisk <mtk.manpages@gmail.com>
3.\"
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.
8.\"
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.
13.\"
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
20.\" professionally.
21.\"
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
24.\" %%%LICENSE_END
25.\"
4b8c67d9 26.TH PTHREAD_ATFORK 3 2017-09-15 "Linux" "Linux Programmer's Manual"
aadc226b
MK
27.SH NAME
28pthread_atfork \- register fork handlers
29.SH SYNOPSIS
30.nf
31.B #include <pthread.h>
dbfe9c70 32.PP
aadc226b
MK
33.BI "int pthread_atfork(void (*" prepare ")(void), void (*" parent ")(void),"
34.BI " void (*" child ")(void));"
35.fi
68e4db0a 36.PP
aadc226b
MK
37Link with \fI\-pthread\fP.
38.SH DESCRIPTION
39The
40.BR pthread_atfork ()
41function registers fork handlers that are to be executed when
42.BR fork (2)
43is called by this thread.
44The handlers are executed in the context of the thread that calls
45.BR fork (2).
847e0d88 46.PP
aadc226b
MK
47Three kinds of handler can be registered:
48.IP * 3
49.IR prepare
50specifies a handler that is executed before
51.BR fork (2)
52processing starts.
53.IP *
54.I parent
55specifies a handler that is executed in the parent process after
56.BR fork (2)
57processing completes.
58.IP *
59.I child
60specifies a handler that is executed in the child process after
61.BR fork (2)
62processing completes.
63.PP
64Any of the three arguments may be NULL if no handler is needed
65in the corresponding phase of
66.BR fork (2)
67processing.
847e0d88 68.PP
aadc226b
MK
69.SH RETURN VALUE
70On success,
71.BR pthread_atfork ()
72returns zero.
73On error, it returns an error number.
74.BR pthread_atfork ()
75may be called multiple times by a thread,
76to register multiple handlers for each phase.
77The handlers for each phase are called in a specified order: the
78.I prepare
79handlers are called in reverse order of registration; the
80.I parent
81and
82.I child
83handlers are called in the order of registration.
84.SH ERRORS
85.TP
86.B ENOMEM
87Could not allocate memory to record the form handler entry.
88.SH CONFORMING TO
89POSIX.1-2001, POSIX.1-2008.
90.SH NOTES
91When
92.BR fork (2)
93is called in a multithreaded process,
94only the calling thread is duplicated in the child process.
95The original intention of
96.BR pthread_atfork ()
97was to allow the calling thread to be returned to a consistent state.
98For example, at the time of the call to
99.BR fork (2),
100other threads may have locked mutexes that are visible in the
101user-space memory duplicated in the child.
102Such mutexes would never be unlocked,
103since the threads that placed the locks are not duplicated in the child.
104The intent of
105.BR pthread_atfork ()
106was to provide a mechanism whereby the application (or a library)
107could ensure that mutexes and other process and thread state would be
108restored to a consistent state.
109In practice, this task is generally too difficult to be practicable.
847e0d88 110.PP
aadc226b
MK
111After a
112.BR fork (2)
113in a multithreaded process returns in the child,
114the child should call only async-signal-safe functions (see
115.BR signal-safety (7))
116until such time as it calls
117.BR execve (2)
118to execute a new program.
847e0d88 119.PP
aadc226b
MK
120POSIX.1 specifies that
121.BR pthread_atfork ()
122shall not fail with the error
123.BR EINTR .
124.SH SEE ALSO
125.BR fork (2),
126.BR atexit (3),
127.BR pthreads (7)