> 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-san-bu-fen-io-xi-tong/9.2.-hang-gui-cheng.md).

# 9.2 行规程

为终端接口所做的大部分字符处理，与终端接入计算机所用硬件设备的类型无关。因此这些处理大多由 tty 驱动（tty driver）或终端处理程序中的公共例程完成。每种硬件接口类型由一个专门的设备驱动支持。硬件驱动是与上一章所述类似的设备驱动，负责对硬件多路复用器编程，负责收发字符，并负责与做输出的进程做部分同步。tty 驱动调用硬件驱动做输出；反过来，硬件驱动把收到的输入字符交给 tty 驱动。既然串行线的用途不止接终端一种，硬件驱动与 tty 驱动之间的模块化接口允许用替代版本替换任一侧。tty 驱动以行规程（line discipline）的身份与系统的其余部分接口。行规程是在异步串行接口（或如后文所见，在这种接口的软件仿真）上提供语义的处理模块，由一个过程式接口——`linesw`（line switch）结构——描述。

`linesw` 结构规定行规程的入口点，恰如字符设备开关 `cdevsw` 列出字符设备驱动的入口点。与所有设备驱动一样，终端多路复用器驱动分为上下两半：上半部在处理系统调用时同步运行，下半部在设备中断发生时异步运行。行规程为终端硬件驱动的上下两半都提供做公共终端处理的例程。行规程的入口点列于表 9.1。

表 9.1 行规程的入口点

| 例程        | 调用方 | 用途        |
| --------- | --- | --------- |
| `l_open`  | 上层  | 初次进入规程    |
| `l_close` | 上层  | 退出规程      |
| `l_read`  | 上层  | 从线路读      |
| `l_write` | 上层  | 向线路写      |
| `l_ioctl` | 上层  | 控制操作      |
| `l_rint`  | 下层  | 收到字符      |
| `l_rend`  | 无   | 未用        |
| `l_meta`  | 无   | 未用        |
| `l_start` | 下层  | 传输完成      |
| `l_modem` | 下层  | 调制解调器载波变迁 |

串行终端接口的设备驱动支持字符设备开关规定的常规字符设备驱动入口点集合。若干标准驱动入口点（`read`、`write` 和 `ioctl`）被调用时立即把控制转给行规程。（标准 tty select 例程 `ttselect()` 通常用作字符设备开关中的设备驱动 `select` 入口。）`open` 和 `close` 例程类似：线路初次进入某规程时——线路初次打开或规程被更改时——调用行规程的 open 入口；相应地，退出规程时调用规程的 close 例程。这些例程都从上层调用，响应相应的系统调用。行规程的其余入口由设备驱动的下半部调用，报告中断时检测到的输入或状态变化：线路上每收到一个字符，就带它调用 `l_rint`（接收器中断）入口。传输完成中断的对应入口是 `l_start` 例程，输出操作完成时调用——这给行规程一个启动更多输出操作的机会；对正常的终端行规程，该例程只是调用驱动的输出例程启动下一块输出。调制解调器控制线的变化（见 9.7 节）可由硬件驱动检测，此时带新状态的指示调用 `l_modem` 例程。

系统含多种不同类型的行规程。多数线路使用下一节描述的两种面向终端的规程之一；其他规程支持串行线上的图形输入板和异步串行网络接口。


---

# 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-san-bu-fen-io-xi-tong/9.2.-hang-gui-cheng.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.
