---
name: "pthread_atfork"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
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(typeof(void (void)) \****prepare***,**                    **typeof(void (void)) \****parent***,**                    **typeof(void (void)) \****child***);**

## DESCRIPTION

The **pthread\_atfork**() function registers fork handlers that are to be executed when [**fork**(2)](/fork/2.md) is called by any thread in a process. The handlers are executed in the context of the thread that calls [**fork**(2)](/fork/2.md).

Three kinds of handler can be registered:

- •

  *prepare* specifies a handler that is executed in the parent process before [**fork**(2)](/fork/2.md) processing starts.

- •

  *parent* specifies a handler that is executed in the parent process after [**fork**(2)](/fork/2.md) processing completes.

- •

  *child* specifies a handler that is executed in the child process after [**fork**(2)](/fork/2.md) processing completes.

Any of the three arguments may be NULL if no handler is needed in the corresponding phase of [**fork**(2)](/fork/2.md) processing.

## RETURN VALUE

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.

## ERRORS

- **ENOMEM**

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

## STANDARDS

POSIX.1-2008.

## HISTORY

POSIX.1-2001.

## NOTES

When [**fork**(2)](/fork/2.md) 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)](/fork/2.md), 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)](/fork/2.md) in a multithreaded process returns in the child, the child should call only async-signal-safe functions (see [**signal-safety**(7)](/signal-safety/7.md)) until such time as it calls [**execve**(2)](/execve/2.md) to execute a new program.

POSIX.1 specifies that **pthread\_atfork**() shall not fail with the error **EINTR**.

## SEE ALSO

[**fork**(2)](/fork/2.md), [**atexit**(3)](/atexit/3.md), [**pthreads**(7)](/pthreads/7.md)
