- NetBSD Manual Pages
PTHREAD_ATFORK(3) NetBSD Library Functions Manual PTHREAD_ATFORK(3)
Powered by man-cgi (2021-06-01).
Maintained for NetBSD
by Kimmo Suominen.
Based on man-cgi by Panagiotis Christias.
pthread_atfork -- register handlers to be called when process forks
Standard C Library (libc, -lc)
pthread_atfork(void (*prepare)(void), void (*parent)(void),
The pthread_atfork() function registers the provided handler functions to
be called when the fork(2) function is called. Each of the three han-
dlers is called at a different place in the fork(2) sequence. The
prepare handler is called in the parent process before the fork happens,
the parent handler is called in the parent process after the fork has
happened, and the child handler is called in the child process after the
fork has happened. The parent and child handlers are called in the order
in which they were registered, while the prepare handlers are called in
reverse of the order in which they were registered.
Any of the handlers given may be NULL.
The intended use of pthread_atfork() is to provide a consistent state to
a child process from a multithreaded parent process where locks may be
acquired and released asynchronously with respect to the fork(2) call.
Each subsystem with locks that are used in a child process should regis-
ter handlers with pthread_atfork() that acquires those locks in the
prepare handler and releases them in the parent handler.
The pthread_atfork() function returns 0 on success and an error number on
The following error code may be returned:
[ENOMEM] Insufficient memory exists to register the fork han-
fork(2), pthread_mutex(3), signal(7)
The pthread_atfork() function conforms to IEEE Std 1003.1c-1995
The pthread_atfork() function first appeared in NetBSD 2.0.
After calling fork(2) from a multithreaded process, it is only safe to
call async-signal-safe functions until calling one of the exec(3) func-
tions. The pthread_*() functions are not async-signal-safe, so it is not
safe to use such functions in the child handler. POSIX does not mandate
that pthread_mutex_unlock() be async-signal-safe, but it is in NetBSD and
thus safe to use within the child handler.
There is no way to unregister a handler registered with pthread_atfork().
NetBSD 9.2 July 18, 2014 NetBSD 9.2