pthread_atfork(3) Library Functions Manual pthread_atfork(3) pthread_atfork - POSIX threads library (libpthread, -lpthread) #include int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void)); The 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). : o prepare specifies a handler that is executed in the parent process before fork(2) processing starts. o parent , fork(2). o child , fork(2). NULL, fork(2). On 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. ENOMEM Could not allocate memory to record the fork handler list entry. POSIX.1-2008. POSIX.1-2001. When 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. fork(2) ; async-signal-safe ( signal-safety(7)) , execve(2) . POSIX.1 , pthread_atfork() EINTR. fork(2), atexit(3), pthreads(7) () Alexey, Azamat Hackimov , kogamatranslator49 , Darima Kogan , Max Is , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . pthread_atfork(3)