---
name: "pthread_atfork"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/pthread_atfork"
---

# pthread\_atfork(3)

register fork handlers

## NAME

**pthread\_atfork** – register fork handlers

## LIBRARY

POSIX Threads Library (libpthread, -lpthread)

## SYNOPSIS

**#include \<pthread.h\>**

*int*\

**pthread\_atfork**(*void \*preparevoid*, *void \*parentvoid*, *void \*childvoid*);

## DESCRIPTION

The **pthread\_atfork**() function declares fork handlers to be called before and after [fork(2)](/fork/2.md), in the context of the thread that called [fork(2)](/fork/2.md).

The handlers registered with **pthread\_atfork**() are called at the moments in time described below:

- *prepare*

  Before [fork(2)](/fork/2.md) processing commences in the parent process. If more than one *prepare* handler is registered they will be called in the opposite order they were registered.

- *parent*

  After [fork(2)](/fork/2.md) completes in the parent process. If more than one *parent* handler is registered they will be called in the same order they were registered.

- *child*

  After [fork(2)](/fork/2.md) processing completes in the child process. If more than one *child* handler is registered they will be called in the same order they were registered.

If no handling is desired at one or more of these three points, a null pointer may be passed as the corresponding fork handler.

## RETURN VALUES

If successful, the **pthread\_atfork**() function will return zero. Otherwise an error number will be returned to indicate the error.

## ERRORS

The **pthread\_atfork**() function will fail if:

- \[ENOMEM\]

  Insufficient table space exists to record the fork handler addresses.

## SEE ALSO

[fork(2)](/fork/2.md), [pthread(3)](/pthread/3.md)

## STANDARDS

The **pthread\_atfork**() function is expected to conform to IEEE Std 1003.1 (“POSIX.1”).

## AUTHORS

This manpage was written by Alex Vasylenko ⟨*lxv@omut.org*⟩.
