> 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/2.2.-nei-he-jie-gou.md).

# 2.2 内核结构

本节从两个视角考察 UNIX 内核的组织：

* 作为静态的软件整体，按组成内核的各模块所提供的功能来归类
* 就其动态运行而言，按提供给用户的服务来归类

内核中最大的部分实现应用程序通过系统调用访问的系统服务。在 4.3BSD 中，这些软件按如下方式组织：

* 基本内核设施：定时器与系统时钟处理、描述符管理、进程管理
* 存储管理支持：调页与交换
* 通用系统接口：在描述符上执行的 I/O、控制和多路复用操作
* 文件系统：文件、目录、路径名翻译、文件加锁和 I/O 缓冲管理
* 终端处理支持：终端接口驱动与终端行规程
* 进程间通信设施：套接字
* 网络通信支持：通信协议与路由等通用网络设施

这些类别中的软件大多与机器无关，可以在不同硬件体系结构间移植。一个主要的例外是最初为 VAX 编写的存储管理支持。它也能移植到其他体系结构（基本上靠仿真 VAX 的存储管理），但为目标体系结构重写通常更好。把系统移植到非 VAX 体系结构的厂商已经这样做了许多次。

内核中与机器相关的部分同主流代码隔离开来。与机器相关的软件包括：

* 最低层的系统启动动作
* 陷阱与故障处理
* 进程运行时上下文的低层操纵
* 硬件设备的配置与初始化
* I/O 设备的运行时支持

表 2.1 概括了 VAX 上 4.3BSD 内核的软件构成。第二列的数字是 C 源码、头文件和汇编语言的行数。内核中的软件几乎全部用 C 语言编写；汇编语言不到 3%。如表 2.2 的统计所示，若不计设备支持，与机器相关的软件只占内核的区区 4.3%。

表 2.1 4.3BSD 内核中与机器无关的软件

| 类别        | 代码行数   | 百分比  |
| --------- | ------ | ---- |
| 头文件       | 6,240  | 5.4  |
| 初始化       | 580    | 0.5  |
| 内核设施      | 5,440  | 4.7  |
| 通用接口      | 1,340  | 1.2  |
| 文件系统      | 6,650  | 5.7  |
| 文件系统磁盘配额  | 1,430  | 1.2  |
| 进程间通信     | 3,500  | 3.0  |
| 网络通信      | 13,440 | 11.5 |
| 终端处理      | 3,410  | 2.9  |
| 虚拟存储      | 4,990  | 4.3  |
| 其他与机器无关部分 | 1,250  | 1.1  |
| 与机器无关部分合计 | 48,270 | 41.5 |

表 2.2 4.3BSD 内核中依赖 VAX 的软件

| 类别         | 代码行数   | 百分比  |
| ---------- | ------ | ---- |
| 输入/输出设备头文件 | 5,725  | 4.9  |
| 输入/输出设备支持  | 36,800 | 31.6 |
| 网络设备头文件    | 2,450  | 2.1  |
| 网络设备支持     | 14,800 | 12.7 |
| 汇编语言例程     | 3,400  | 2.9  |
| 其他与机器相关部分  | 5,025  | 4.3  |
| 与机器相关部分合计  | 68,200 | 58.5 |

内核只有一小部分用于系统的初始化。这部分代码在系统自举进入运行状态时使用，负责建立内核的硬件与软件环境（见第 13 章）。有些操作系统（特别是物理内存有限的那些）会在这些功能执行完毕后丢弃或覆盖执行它们的软件。4.3BSD 内核并不回收启动代码占用的内存，因为这点内存空间在典型机器的全部资源中只占很小的比例；另外，启动代码在内核中并非集中一处——它散布各处，而且通常出现在与被初始化对象逻辑相关的位置。不过，DEC 的 Ultrix——一个源自 4.2BSD 的系统——确实回收这部分代码。


---

# 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/2.2.-nei-he-jie-gou.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.
