Declares handlers to be called when the process forks a child.
This routine is for Digital UNIX systems only.
Syntax
pthread_atfork(
prepare,
parent,
child );
Argument Data Type Access
prepare Handler read
parent Handler read
child Handler read
C Binding
#include
int
pthread_atfork (
void (*prepare)(void),
void (*parent)(void),
void (*child)(void) );
Arguments
- prepare
- Address of a procedure that performs the fork preparation
handling. This routine is called in the parent process before
creating the child.
- parent
- Address of a procedure that performs the fork parent handling.
This routine is called in the parent process after creating the
child and before returning to the caller of fork(2).
- child
- Address of a procedure that performs the fork child handling.
This routine is called in the child process before returning to the
caller of fork(2).
Description
This routine allows a main program or library to control
resources during a UNIX fork(2) operation by declaring fork
handlers that are called before and after the fork(2) execution.
The prepare fork handler is called before the fork(2)
execution. The parent handler is called after the fork(2) execution
within the parent process, and the child handler is called after
fork(2) in the new child process. If no handling is desired, you can
set any of the arguments to a NULL.
- Note
- It is not legal to call
pthread_atfork from within a handler routine. Doing so could cause a
deadlock.
Mutex locks might be held by threads that no longer exist in a child
process and any associated state might be inconsistent. The program
can avoid this problem by calling pthread_atfork() to provide code
that acquires and releases locks critical to the child process. For
example, if your library uses a mutex named "my_mutex", you might
provide pthread_atfork handlers like:
void my_prepare(void)
{
pthread_mutex_lock(&my_mutex);
}
void my_parent(void)
{
pthread_mutex_unlock(&my_mutex);
}
void my_child(void)
{
pthread_mutex_unlock(&my_mutex);
/* Reinitialize state that doesn't apply...like heap owned */
/* by other threads */
}
{
.
.
.
pthread_atfork(my_prepare, my_parent, my_child);
.
.
fork();
}
Return Values
If an error occurs, this routine returns an integer indicating the
type of error. Possible return values are as follows:
Return Description
0 Successful completion.
[ENOMEM] Insufficient table space exists to record the fork
handler addresses.
Associated Routines