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 before fork(2) processing starts ๐ŸŒฑ
  • parent โ€” a handler that gets executed in the parent process after fork(2) processing completes โœจ
  • child โ€” a handler that gets executed in the child process after fork(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 prepare handlers are called in reverse order of registration
  • the parent and child handlers 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 a fork(2) in a multithreaded process returns in the child, the child should call only async-signal-safe functions (check signal-safety(7)) until such time as it calls execve(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms