> 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/4.3-bsd-unix-cao-zuo-xi-tong-she-ji-yu-shi-xian/di-er-bu-fen-jin-cheng/4.5.-jin-cheng-chuang-jian.md).

# 4.5 进程创建

UNIX 中，新进程用 `fork` 系统调用创建。另有 `vfork` 系统调用，与 `fork` 的差别在于对虚拟内存资源的处理方式；`vfork` 在第 5 章描述。

由 `fork` 创建的进程称为原父进程的子进程。在用户看来，子进程是父进程的精确副本，只有两个值例外：PID 和父进程 PID。`fork` 调用向父进程返回子进程的 PID，向子进程返回零。程序由此可以在 fork 之后凭返回值判断自己是父进程还是子进程。

一次 `fork` 涉及三个主要步骤：

1. 为子进程分配并初始化一个新的 proc 结构
2. 为子进程复制父进程上下文（包括 user 结构和虚拟内存资源）
3. 调度子进程运行

第二步与第 5 章描述的存储管理设施的操作密切相关。因此这里只描述与进程管理相关的动作。

系统维护一张空闲进程表项的链表，查看链表头 `freeproc` 即可找到未用的表项。

创建新进程期间，系统根据父进程的 proc 结构初始化子进程 proc 结构的大部分内容。有一个值不传播：PID——它必须在所有进程中唯一。早期 UNIX 靠线性搜索进程表来验证 PID 的唯一性。这个搜索在进程众多的大系统上行不通。4.3BSD 在 `mpid` 与 `pidchecked` 之间维护一段未分配的 PID。新 PID 取 `mpid` 的值使用，然后 `mpid` 递增。`mpid` 追上 `pidchecked` 时，系统扫描一遍所有现存进程（不只是活跃的——僵尸进程和被换出的进程也查），算出一段新的未用 PID。

子进程继承父进程的全部特权和限制。除 u. 区中的打开文件等信息外，子进程的 proc 表项还从父进程继承以下信息：

* 信号状态——投递信号用的 UID、进程组和信号动作
* `p_nice` 调度参数
* 程序段信息——各段的大小和页表；若使用共享正文段，还有指向相关正文表项的指针
* 内存中的磁盘配额信息（见 7.6 节）

复制进程映像要通过调用 `procdup()` 来使用存储管理设施。`procdup()` 例程接受一个指向子进程已初始化 proc 结构的指针，负责分配子进程执行所需的全部资源。`procdup()` 的调用在子进程中返回 1，在父进程中返回 0。内核靠在子进程首次投入运行时安排一次非局部跳转来返回不同的值。`procdup()` 被调用时，填写子进程 PCB 的 `pcb_sswap` 字段，然后调用 `savectx()`，传入子进程 user 结构中 `u_ssave` 的地址：

```c
procdup(p, isvfork)
    struct proc *p;
{
    ...             /* 分配并初始化子进程的 u */
    childu.u_pcb.pcb_sswap = &childu.u_ssave;
    if (savectx(&childu.u_ssave))
        return (1); /* 在子进程中返回 1 */
    ...             /* 复制进程映像 */
    return (0);
}
```

`resume()` 例程在每次上下文切换后检查 PCB 软件扩展中的 `pcb_sswap` 字段。该字段非零时，`resume()` 调用 `ldctx()` 例程，把 `pcb_sswap` 中找到的值作为非局部跳转的位置传入，把控制交回 `procdup()` 中当初调用 `savectx()` 的那一点，但带回另一个返回值。这一串动作使 `procdup()` 依据自己运行在子进程还是父进程的上下文中，向调用者返回不同的值。

最后，新子进程放上运行队列，为调度器所知；`procdup()` 的返回值传回，成为 `fork` 系统调用的返回值。


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://book.bsdcn.org/4.3-bsd-unix-cao-zuo-xi-tong-she-ji-yu-shi-xian/di-er-bu-fen-jin-cheng/4.5.-jin-cheng-chuang-jian.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
