pthread_atfork(3)
๐ pthread_atfork(3) ๐
okay bestie, welcome to the Library Functions Manual section for pthread_atfork(3) โ yes that's the manual name, it's basically her last name, we're on a first-name basis now ๐ซถ
โจ NAME โจ
pthread_atfork โ register fork handlers ๐ธ
๐ LIBRARY ๐
POSIX threads library โ that's libpthread, link it with -lpthread ๐ง
๐ SYNOPSIS ๐
#include <pthread.h>
int pthread_atfork(typeof(void (void)) *prepare,
typeof(void (void)) *parent,
typeof(void (void)) *child);
๐ฆ DESCRIPTION ๐ฆ
so pthread_atfork() registers fork handlers, and those handlers get executed whenever fork(2) is called by any thread in the process. and like, not in some random thread โ they run in the context of the exact thread that calls fork(2). main character energy, honestly ๐
there are three kinds of handler you can register:
prepareโ a handler that gets executed in the parent process beforefork(2)processing starts ๐ฑparentโ a handler that gets executed in the parent process afterfork(2)processing completes โจchildโ a handler that gets executed in the child process afterfork(2)processing completes ๐
and here's the slay: any of those three arguments can be NULL if you don't need a handler for that phase of fork(2) processing. no handler, no problem, she's flexible ๐
๐ฅ RETURN VALUE ๐ฅ
on success, pthread_atfork() returns zero. on error, it returns an error number โ note that it's an error number, not a -1 situation, iykyk.
also a process can call pthread_atfork() multiple times to register extra handlers, and the order is very specific, bestie, so listen up:
- the
preparehandlers are called in reverse order of registration - the
parentandchildhandlers are called in the order of registration
that's not a vibe, that's a rule ๐
๐ญ ERRORS ๐ญ
ENOMEMโ could not allocate memory to record that fork handler list entry. girl is out of memory, it's giving tragic ๐
๐ STANDARDS ๐
POSIX.1-2008. periodt.
๐ HISTORY ๐
POSIX.1-2001. she's been around, she's a legacy icon โญ
๐ NOTES ๐
okay this part is important so like, focus bestie ๐ซถ
when fork(2) is called in a multithreaded process, only the calling thread gets duplicated in the child process. that's it. just her. the others didn't make the cut ๐
the original intention of pthread_atfork() was to let the child process be returned to a consistent state. think about it: at the moment fork(2) is called, other threads might have locked mutexes that are visible in the user-space memory that got duplicated into the child. and those mutexes would never get unlocked, because the threads that placed those locks were not duplicated in the child. like, the key-holder didn't get invited to the party ๐
so the intent of pthread_atfork() was to give the application (or a library) a mechanism to make sure mutexes and other process and thread state could be restored to a consistent state. but in practice? this task is generally too difficult to be practicable. she tried, okay. she really tried ๐ญ
and here's the warning, no cap:
after afork(2)in a multithreaded process returns in the child, the child should call only async-signal-safe functions (checksignal-safety(7)) until such time as it callsexecve(2)to execute a new program. only those. nothing else. do not pass go ๐ซ
also POSIX.1 specifies that pthread_atfork() shall not fail with the error EINTR. noted, recorded, respected โจ
๐ธ SEE ALSO ๐ธ
and that's the whole page, bestie ๐ Linux man-pages 6.20, dated 2026-02-08 โ mwah, love that for us, go forth and fork responsibly ๐๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2017, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms