Section: Linux Programmer's Manual (3)
pthread_atfork - register fork handlers
int pthread_atfork(void (*prepare)(void), void (*parent)(void),
Link with -pthread.
function registers fork handlers that are to be executed when
is called by this thread.
The handlers are executed in the context of the thread that calls
Three kinds of handler can be registered:
specifies a handler that is executed before
specifies a handler that is executed in the parent process after
specifies a handler that is executed in the child process after
Any of the three arguments may be NULL if no handler is needed
in the corresponding phase of
On error, it returns an error number.
may be called multiple times by a thread,
to register multiple handlers for each phase.
The handlers for each phase are called in a specified order: the
handlers are called in reverse order of registration; the
handlers are called in the order of registration.
Could not allocate memory to record the form handler entry.
is called in a multithreaded process,
only the calling thread is duplicated in the child process.
The original intention of
was to allow the calling thread to be returned to a consistent state.
For example, at the time of the call to
other threads may have locked mutexes that are visible in the
user-space memory duplicated in the child.
Such mutexes would never be unlocked,
since the threads that placed the locks are not duplicated in the child.
The intent of
was to provide a mechanism whereby the application (or a library)
could ensure that mutexes and other process and thread state would be
restored to a consistent state.
In practice, this task is generally too difficult to be practicable.
in a multithreaded process returns in the child,
the child should call only async-signal-safe functions (see
until such time as it calls
to execute a new program.
POSIX.1 specifies that
shall not fail with the error
This page is part of release 5.07 of the Linux
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at