> 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/5.10.-jin-cheng-de-zhong-zhi.md).

# 5.10 进程的终止

与虚拟内存系统运作相关的进程状态最后一变是 `exit`；这个系统调用按第 4 章所述终止进程。这里讨论 `exit` 中释放进程虚拟内存资源的部分。释放分三步：

1. 释放地址空间的用户部分，内存和交换空间上都释放
2. 释放 user 结构
3. 释放页表资源

后两步有些复杂，因为 user 结构及其映射要一直用到进程最后一次交出处理器。

第一步，释放用户地址空间，与 `exec` 期间释放旧地址空间的操作相同。两种情况下，若进程是 `vfork` 的子进程，这一步都不需要：地址空间交还父进程。首先释放进程的正文节。若内存中还有其他进程连着这份正文，引用计数简单减一，进程结构中的正文指针清零。若内存中没有这份正文映像的其他使用者，正文映像被释放；若尚未写入交换，先把它换出。映像为仍连着它的被换出进程保留，也为映像不久后可能被重新挂接而保留。正文区的页被释放；当时可能正参与页面换出操作的页标记为 gone（已走），而不是立即释放；脏页清理例程会注意到页已走，待其 I/O 完成后把页放上空闲表（见 5.12 节）。数据页和栈页同样释放。接下来释放交换空间，但若有进程页正在换出，这一步必须等。否则，涉及的交换块可能在页面换出发生之前就被重新分配使用，毁掉新使用者的交换映像。为此，系统在每个进程的 proc 结构中保存其进行中的页面换出操作计数。任何页面换出完成后，数据和栈交换映射中的交换块才被释放。

接下来必须释放 user 结构占用的内存。麻烦的时刻从此开始：进程要释放自己还没用完的资源。这一处境使释放内存的惯常做法有两点改变。第一，映射 user 结构的页表复制到一块临时区域，使 `vmemfree()` 释放内存时，映射 user 结构的页表上的有效位和保护位不被清除。第二，若 user 结构或内核栈的页在进程完成 `exit()` 之前就被重新分配使用，后果是灾难性的。内存既可能被缺页处理程序同步分配，也可能在分组到达时被网络从中断级异步分配（见第 11 章）。要阻止一切内存分配，必须提升处理器中断优先级以延迟网络中断。进程随后可以释放自己 user 结构的页，放心它们在交出处理器之前不会被重用。下一次上下文切换会降低优先级，中断恢复。

释放进程资源的最后一步是释放页表。装页表的内存用映射这些页表的系统 PTE 的地址经 `vmemfree()` 释放；系统 PTE 再经 `rmfree()` 归还给其资源映射 `kernelmap`。

资源全部释放后，退出进程最后处理自己的 proc 表项，脱离进程组，并通知父进程自己已完成。进程就此成为僵尸进程——一个没有资源、连内核栈都没有的进程。父进程将用 `wait` 调用收取它的退出状态，并释放它的进程结构。

调用 `wait` 时虚拟内存系统无事可做：进程的页和页表在执行 `exit` 时已经移除。`wait` 时，系统只把进程状态返回给调用者，释放进程表项和存放资源使用信息的那一小块空间。


---

# 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/5.10.-jin-cheng-de-zhong-zhi.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.
