pthread_atfork_np subroutine`

Purpose

Registers fork handlers.

Library

Threads Library (libpthreads.a)

Syntax

#include <sys/types.h>
#include <unistd.h>

int pthread_atfork_np (arg, prepare, parent, child)
void *arg;
void (*prepare)(void *);
void (*parent)(void *);
void (*child)(void *);

Description

The pthread_atfork_np subroutine registers cleanup handlers for the fork subroutine. The arg is the parameter to be passed to the functions for pre and post fork handling. The prepare handler is called before the processing of the fork subroutine commences. The parent handler is called after the processing of the fork subroutine completes in the parent process. The child handler is called after the processing of the fork subroutine completes in the child process.

When the fork subroutine is called, only the calling thread is duplicated in the child process, but all synchronization variables are duplicated. The pthread_atfork_np subroutine provides a way to prevent state inconsistencies and resulting deadlocks. The expected usage is that the prepare handler acquires all mutexes, and the two other handlers release them in the parent and child processes.

The prepare handlers are called in LIFO (Last In First Out) order; whereas the parent and child handlers are called in FIFO (first-in first-out) order. Therefore, the order of calls to the pthread_atfork_np subroutine is significant.

Note:
  • The pthread.h header file must be the first included file of each source file using the threads library.
  • The pthread_atfork_np subroutine is not portable.

Paramaters

arg
Points to the parameter to be passed to the fork cleanup handlers.
prepare
The pre-fork cleanup handler. If no pre-fork handling is desired, the value of this pointer should be set to NULL.
parent
The parent post-fork cleanup handler. If no parent post-fork handling is desired, the value of this pointer should be set to NULL.
child
The child post-fork cleanup handler. If no child post-fork handling is desired, the value of this pointer should be set to NULL.

Return Values

Upon successful completion, the pthread_atfork_np subroutine returns a value of zero. Otherwise, an error number is returned to indicate the error

Error Codes

ENOMEM
Insufficient table space exists to record the fork handler addresses.