> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/pthread/pthread_atfork.3.md).

# pthread\_atfork.3

`pthread_atfork` — 注册 fork 处理程序

## 名称

`pthread_atfork`

## 库

Lb libpthread

## 概要

`#include <pthread.h>`

`Ft int Fo pthread_atfork void *preparevoid void *parentvoid void *childvoid Fc`

## 描述

`Fn pthread_atfork` 函数声明在 fork(2) 之前和之后调用的 fork 处理程序，这些处理程序在调用 fork(2) 的线程上下文中执行。

通过 `Fn pthread_atfork` 注册的处理程序会在以下时机被调用：

**`prepare`** 在父进程中开始 fork(2) 处理之前。如果注册了多个 `prepare` 处理程序，它们将按注册顺序的相反顺序被调用。

**`parent`** 在父进程中 fork(2) 完成之后。如果注册了多个 `parent` 处理程序，它们将按注册顺序被调用。

**`child`** 在子进程中 fork(2) 处理完成之后。如果注册了多个 `child` 处理程序，它们将按注册顺序被调用。

如果在上述三个时机中的一个或多个不需要任何处理，可以传入空指针作为对应的 fork 处理程序。

## 返回值

若成功，`Fn pthread_atfork` 函数将返回零。否则将返回一个错误号以指示错误。

## 错误

`Fn pthread_atfork` 函数在以下情况下会失败：

**\[Er** ENOMEM] 没有足够的表空间来记录 fork 处理程序地址。

## 参见

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

## 标准

`Fn pthread_atfork` 函数预期遵循 IEEE Std 1003.1 ("POSIX.1") 标准。

## 作者

本手册页由 Alex Vasylenko <lxv@omut.org> 编写。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/pthread/pthread_atfork.3.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
