> 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.9.-xi-tong-yun-xing.md).

# 3.9 系统运行

有若干与系统启动和停机有关的运行管理功能。自举操作在 13.2 节描述，系统停机在 13.5 节描述。

**记账**

系统支持一种简单形式的资源记账。每个进程终止时，一条描述该进程所用资源的记账记录被写入全系统范围的记账文件。系统提供的信息包括：

* 所运行命令的名字
* 使用的用户和系统 CPU 时间
* 命令运行的经历时间
* 使用的平均内存量
* 完成的磁盘 I/O 操作次数
* 进程的 UID 和 GID
* 进程启动自哪个终端

记账记录中的信息取自 3.8 节描述的运行时统计。时间字段的粒度是六十四分之一秒。为节省记账文件的空间，时间以 16 位字存储成浮点数：3 位作以 8 为底的指数，其余 13 位作小数部分。出于历史原因，磁盘操作计数也经同一浮点转换例程处理，因此磁盘操作数在转换成浮点表示之前先乘以 64。

还有一些布尔标志，标明进程如何终止、是否使用过超级用户特权、`fork` 之后是否用过 `exec`。

超级用户把记账文件的名字传给内核来启动记账。内核在终止进程时把一条记账记录追加到记账文件。内核自己不用记账记录；记录的汇总和使用完全是用户级记账程序的事。为防记账文件不受控增长把文件系统撑爆，当文件系统只剩 2% 空闲空间时系统暂停记账，待空闲空间恢复到至少 4% 再继续。

记账信息有若干局限。运行时间和内存使用的信息只是近似值，因为它们是统计采集的。记账信息只在进程退出时写入，系统意外停机时仍在运行的进程不会出现在记账文件里——这些未记账的进程包括所有长寿的系统守护进程。最后，记账记录缺少精确计费所需的许多信息，例如磁带机、打印机和网络通信等其他资源的使用。

## 习题

3.1 描述三种系统活动。

3.2 内核上半部执行的例程何时可能被抢占？何时可能被中断？

3.3 为什么内核下半部执行的例程不能用位于用户区中的信息？

3.4 系统为什么要把尽可能多的工作从高优先级中断推迟到较低优先级的软件中断进程？

3.5 用户进程设置计时器时能请求的最短（非零）时间由什么决定？

3.6 内核如何确定自己被调用来执行的是哪个系统调用？

3.7 已初始化数据在可执行文件中如何表示？未初始化数据如何表示？为什么两种表示不同？

3.8 为什么从 UTC 到本地时间的换算由用户进程而不是在内核中完成？

3.9 描述程序启动时不把栈区零填充的安全影响。

3.10 描述如何用 “#!” 机制让需要仿真的程序看起来像普通可执行文件。

3.11 一个文件能否把权限设成属主读不了、某个组却能读？若属主恰属能读的组，又当如何？

\*3.12 由内核而不是由应用重启被中断的系统调用，好处是什么？

\*3.13 描述一种 callout 队列所用“排序差值”算法表现不佳的场景。针对你的场景给出一种比排序差值算法跑得更快的替代数据结构。

\*3.14 SIGPROF 剖析计时器本来想取代 `profil` 系统调用来采集程序计数器的统计采样。给出必须保留 `profil` 设施的两条理由。

\*3.15 交换了自己有效 UID 和实际 UID 的 setuid 程序，应当被允许再换回来吗？为什么？

\*3.16 进程记账机制的什么弱点使它不适合在商业环境中使用？

## 参考文献

Barkley & Lee, 1988. R. E. Barkley & T. P. Lee, “A Heap-Based Callout Implementation to Meet Real-Time Needs,” *USENIX Association Conference Proceedings*, pp. 213–222 (June 1988).

Gifford, 1981. D. Gifford, “Information Storage in a Decentralized Computer System,” PhD Thesis, Electrical Engineering Department, Stanford University, Stanford, CA (1981).

Gusella et al., 1986. R. Gusella, S. Zatti, & J. M. Bloom, “The Berkeley UNIX Time Synchronization Protocol,” pp. 22:1–10 in *UNIX System Manager's Manual, 4.3 Berkeley Software Distribution, Virtual VAX-11 Version,* USENIX Association, Berkeley, CA (1986).

Ritchie, 1979. D. M. Ritchie, “Protection of Data File Contents,” *United States Patent* (4,135,240), United States Patent Office (January 16, 1979). Assignee: Bell Telephone Laboratories, Inc., Murray Hill, NJ, Appl. No.: 377,591, Filed: Jul. 9, 1973.

Varghese & Lauck, 1987. G. Varghese & T. Lauck, “Hashed and Hierarchical Timing Wheels: Data Structures for the Efficient Implementation of a Timer Facility,” *Proceedings of the Eleventh Symposium on Operating Systems Principles,* pp. 25–38 (November 1987).


---

# 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.9.-xi-tong-yun-xing.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.
