pthread_atfork()

Updated: April 19, 2023

Register fork handlers

Synopsis:

#include <pthread.h>

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

Arguments:

prepare
NULL, or a pointer to the handler to call before the fork.
parent
NULL, or a pointer to the handler to call after the fork in the parent process.
child
NULL, or a pointer to the handler to call after the fork in the child process.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The pthread_atfork() function registers fork handler functions to be called before and after a fork(), in the context of the thread that called fork(). You can set one or more of the arguments to NULL to indicate no handler.

You can register multiple prepare, parent, and child fork handlers, by making additional calls to pthread_atfork(). In this case, the parent and child handlers are called in the order they were registered, and the prepare handlers are called in the reverse order.

Note:
  • In the child handler, you can call only functions that are async-signal-safe (i.e., functions that you can safely call from a signal handler).
  • It's possible to call fork() from a multithreaded process, but it can be very difficult to do so safely, so we recommend that you call it only from single-threaded processes. For more information, see Using fork() in a multithreaded process in the “Processes and Threads” chapter of Getting Started with QNX Neutrino.

Returns:

EOK
Success.
ENOMEM
Insufficient memory to record fork handlers.

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes