Open System Services System Calls Reference Manual (G06.29+, H06.08+, J06.03+)
System Functions (n - p) pthread_atfork(2)
NAME
pthread_atfork - Declares fork-handler routines to be called when the calling thread’s process
forks a child process
LIBRARY
G-series native OSS processes: /G/system/sysnn/zsptsrl
32-bit H-series and J-series OSS processes that use the POSIX User Thread Model library:
/G/system/zdllnnn/zputdll
64-bit H-series and J-series OSS processes that use the POSIX User Thread Model library:
/G/system/zdllnnn/yputdll
H-series and J-series OSS processes that use the Standard POSIX Threads library:
/G/system/zdllnnn/zsptdll
SYNOPSIS
#include <pthread.h> | #include <spthread.h>
/* pthread.h is required to use POSIX User Thread Model library */
/* spthread.h is required to use Standard POSIX Threads library */
int pthread_atfork(
void (*prepare) (void),
void (*parent) (void),
void (*child) (void));
PARAMETERS
prepare Specifies the address of a routine that performs the fork preparation handling.
This routine is called in the parent process before the child process is created.
parent Specifies the address of a routine that performs the fork parent handling. This
routine is called in the parent process after the child process is created and
before the return to the caller of fork( ).
child Specifies the address of a routine that performs the fork child handling. This rou-
tine is called in the child process before the return to the caller of fork().
DESCRIPTION
This function allows a main program or library to control resources during a fork() operation by
declaring fork-handler routines, as follows:
• The fork-handler routine specified by the prepare parameter is called before fork() exe-
cutes.
• The fork-handler routine specified by the parent parameter is called after fork() executes
within the parent process.
• The fork-handler routine specified by the child parameter is called in the new child pro-
cess after fork() executes.
Your program (or library) can use fork handlers to ensure that program context in the child pro-
cess is consistent and meaningful. After fork() executes, only the calling thread exists in the
child process, and the state of all memory in the parent process is replicated in the child process,
including the states of any mutexes, condition variables, and so on.
For example, in the new child process there might exist locked mutexes that are copies of
mutexes that were locked in the parent process by threads that do not exist in the child process.
Therefore, any associated program state might be inconsistent in the child process.
The program can avoid this problem by calling pthread_atfork() to provide routines that
acquire and release resources that are critical to the child process. For example, the prepare
handler should lock all mutexes that you want to be usable in the child process. The pare nt
527186-023 Hewlett-Packard Company 5−29