pthread_atfork()

register fork handlers 

Function


SYNOPSIS

#include <pthread.h>

int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child) (void));


DESCRIPTION

The pthread_atfork() function establishes handlers to be invoked before and after a call to fork() in the context of the thread that called fork(). The order of calls to pthread_atfork() is significant. Functions specified as prepare handlers are invoked in last-in-first-out (LIFO) order when the fork() function is called. The parent and child handlers are invoked in first-in-first-out (FIFO) order in the context of the parent and child processes, respectively, prior to fork() returning to the caller. Any of these functions may be NULL.

The order of invocation of at-fork handlers can be used to ensure that locking hierarchy is maintained across a call to fork().


PARAMETERS

prepare 

Specifies a function to be run before a fork() function. This parameter may be NULL, in which case no function is added to the list of prepare handlers. Prepare handlers are run in last-in-first-out order with respect to calls to pthread_atfork().

parent 

Specifies a function to be invoked in the parent process prior to fork() returning. This parameter may be NULL, in which case no function is added to the list of parent handlers. Parent handlers are run in first-in-first-out order with respect to calls to pthread_atfork().

child 

Specifies a function to be invoked in the child process prior to fork() returning. This parameter may be NULL, in which case no function is added to the list of child handlers. Child handlers are run in first-in-first-out order with respect to calls to pthread_atfork().


RETURN VALUES

On success, pthread_atfork() returns 0. On error, one of the following values is returned:

ENOMEM 

Insufficient memory exists to record the at-fork handler functions.


CONFORMANCE

POSIX P1003.1 (1996)


MULTITHREAD SAFETY LEVEL

MT-safe.


PORTING ISSUES

Handlers established with pthread_atfork() are not invoked when one of the _NutForkExec() family of functions is called, because no child process is actually created. In addition, handlers established with pthread_atfork() are not run when vfork() is called, because parent and child are sharing the address space.

Handlers established with pthread_atfork() from a dynamically-loaded library are removed when that library is unloaded.


AVAILABILITY

PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition


SEE ALSO

Functions:
_NutForkExecl(), _NutForkExecle(), _NutForkExeclp(), _NutForkExeclpe(), _NutForkExecv(), _NutForkExecve(), _NutForkExecvp(), _NutForkExecvpe(), fork(), vfork()


PTC MKS Toolkit 10.4 Documentation Build 39.