pthread_atfork - Online Linux Manual PageSection : 3
Updated : 2023-02-05
Source : Linux man-pages 6.03

NAMEpthread_atfork − register fork handlers

LIBRARYPOSIX threads library (libpthread, −lpthread)

SYNOPSIS#include <pthread.h>int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void));

DESCRIPTIONThe pthread_atfork() function registers fork handlers that are to be executed when fork(2) is called by any thread in a process. The handlers are executed in the context of the thread that calls fork(2). Three kinds of handler can be registered: • prepare specifies a handler that is executed in the parent process before fork(2) processing starts. • parent specifies a handler that is executed in the parent process after fork(2) processing completes. • child specifies a handler that is executed in the child process after fork(2) processing completes. Any of the three arguments may be NULL if no handler is needed in the corresponding phase of fork(2) processing.

RETURN VALUEOn success, pthread_atfork() returns zero. On error, it returns an error number. pthread_atfork() may be called multiple times by a process to register additional handlers. The handlers for each phase are called in a specified order: the prepare handlers are called in reverse order of registration; the parent and child handlers are called in the order of registration.

ERRORSENOMEM  Could not allocate memory to record the fork handler list entry.

STANDARDSPOSIX.1-2001, POSIX.1-2008.

NOTESWhen fork(2) is called in a multithreaded process, only the calling thread is duplicated in the child process. The original intention of pthread_atfork() was to allow the child process to be returned to a consistent state. For example, at the time of the call to fork(2), other threads may have locked mutexes that are visible in the user-space memory duplicated in the child. Such mutexes would never be unlocked, since the threads that placed the locks are not duplicated in the child. The intent of pthread_atfork() was to provide a mechanism whereby the application (or a library) could ensure that mutexes and other process and thread state would be restored to a consistent state. In practice, this task is generally too difficult to be practicable. After a fork(2) in a multithreaded process returns in the child, the child should call only async-signal-safe functions (see signal−safety(7)) until such time as it calls execve(2) to execute a new program. POSIX.1 specifies that pthread_atfork() shall not fail with the error EINTR.

SEE ALSOfork(2), atexit(3), pthreads(7)
0
Johanes Gumabo
Data Size   :   9,844 byte
man-pthread_atfork.3Build   :   2024-12-05, 20:55   :  
Visitor Screen   :   x
Visitor Counter ( page / site )   :   3 / 187,424
Visitor ID   :     :  
Visitor IP   :   3.15.34.105   :  
Visitor Provider   :   AMAZON-02   :  
Provider Position ( lat x lon )   :   39.962500 x -83.006100   :   x
Provider Accuracy Radius ( km )   :   1000   :  
Provider City   :   Columbus   :  
Provider Province   :   Ohio ,   :   ,
Provider Country   :   United States   :  
Provider Continent   :   North America   :  
Visitor Recorder   :   Version   :  
Visitor Recorder   :   Library   :  
Online Linux Manual Page   :   Version   :   Online Linux Manual Page - Fedora.40 - march=x86-64 - mtune=generic - 24.12.05
Online Linux Manual Page   :   Library   :   lib_c - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Online Linux Manual Page   :   Library   :   lib_m - 24.10.03 - march=x86-64 - mtune=generic - Fedora.40
Data Base   :   Version   :   Online Linux Manual Page Database - 24.04.13 - march=x86-64 - mtune=generic - fedora-38
Data Base   :   Library   :   lib_c - 23.02.07 - march=x86-64 - mtune=generic - fedora.36

Very long time ago, I have the best tutor, Wenzel Svojanovsky . If someone knows the email address of Wenzel Svojanovsky , please send an email to johanes_gumabo@yahoo.co.id .
If error, please print screen and send to johanes_gumabo@yahoo.co.id
Under development. Support me via PayPal.