> 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-yi-bu-fen-gai-lan/3.8.-zi-yuan-kong-zhi.md).

# 3.8 资源控制

所有系统都受硬件体系结构和配置的制约；为保证正常运行，不得超越这些限度。至少必须把硬件限度施加给在系统上运行的进程。有时还可能希望把进程限制得比硬件限度更严。系统度量资源使用，并允许把消耗限制在硬件限度上或之下。

**进程优先级**

系统把 CPU 调度优先级给予近期未用 CPU 时间的进程。这种优先级方案偏向交互式进程和每次只执行一小会儿的进程。为每个进程选定的优先级由内核内部维护。优先级的计算受每进程 `nice` 变量影响：`nice` 为正值表示进程愿意少占处理机份额，为负值表示应多给份额。多数进程以默认的 `nice` 零运行，既不多占也不少占。可以查询或更改当前指派给一个进程、一个进程组或指定用户的全部进程的 `nice` 值。除 `nice` 之外，调度还受许多别的因素影响，包括进程近期使用的 CPU 时间量、近期使用的内存量，以及系统当前负载。所用的具体算法见 4.4 节。

**资源使用**

进程执行时使用 CPU、内存等系统资源。内核跟踪每个进程使用的资源，汇编描述这种使用的统计信息。内核管理的统计信息在进程执行期间可供进程查询；进程终止后，统计信息交给它的父进程。

进程使用的资源由系统调用 `getrusage` 返回，可以查询当前进程的，也可以查询它全部已终止子进程的。信息包括：

* 进程使用的用户时间和系统时间
* 进程的内存使用
* 进程的调页与磁盘 I/O 活动
* 进程的自愿与非自愿上下文切换次数
* 进程做的进程间通信量

资源使用信息在内核各处采集。CPU 时间在 `hardclock()` 的每个节拍采集。内核每秒采样一次活动进程的内存使用量来计算内存利用；调度程序在重算进程优先级的同一时刻做内存采样。`pagein()` 例程在每次为满足调页请求而启动磁盘传输时重算调页活动（见 5.11 节）。I/O 活动统计在进程每次为满足文件或设备 I/O 请求而启动传输时采集。IPC 通信活动在每次经套接字接口发送分组时计数。

**资源限制**

内核控制某些每进程资源的使用。这些控制包括：

* 可累计的 CPU 时间上限
* 可创建文件的大小上限
* 进程数据段的大小上限
* 进程栈段的大小上限
* core 文件的大小上限
* 进程任一时刻可用的物理内存上限

内核控制的每种资源都维护两个限度：软限制（soft limit）和硬限制（hard limit）。只有超级用户能提高硬限制。其他用户可以在 0 到相应硬限制的范围内调整软限制，或者（不可逆地）只降低硬限制。进程超过某些软限制时，内核向它投递信号，通知它超过了资源限制。这种信号正常情况下会终止进程，但进程也可以选择捕获或忽略该信号。

限度在与资源统计相同的地点执行。CPU 时间限制在 `hardclock()` 中执行；物理内存限制由 `pageout()` 执行——进程超过物理内存限制期间，`pageout()` 例程见到它的任何页都会拿走。栈和数据段的限制在到达限度后以分配失败返回执行。文件大小限制由文件系统执行：一旦到达文件大小限制，就给进程发信号，默认动作是杀死进程；若进程捕获或忽略该信号并继续写，就会得到错误返回。

**文件系统配额**

除了对单个文件大小的限制，内核还会对用户在文件系统上能使用的空间总量施加限制。这些限制的实现留到 7.6 节讨论。


---

# 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-yi-bu-fen-gai-lan/3.8.-zi-yuan-kong-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.
