> 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.1.-zhong-duan-chu-li-mo-shi.md).

# 9.1 终端处理模式

UNIX 系统上最常见的硬件外设之一，是支持一台或多台终端的接口。最常见的接口类型是终端多路复用器（terminal multiplexer）——一种连接多条异步 RS-232 串行线的设备，串行线上可以接终端、调制解调器、打印机及类似设备。与第 8 章描述的块存储设备和第 11 章将考察的网络设备不同，终端设备常常一次处理一个字符的数据。与其他设备一样，终端多路复用器由针对实际硬件的设备驱动控制。

终端接口异步中断处理器呈交输入，与进程读取用户输入的请求无关。数据收到即处理，然后保存起来，直到有进程来请求——这就允许了预键入（type-ahead）。多数终端端口用来连接本地或远程终端，供用户登录系统。这样使用时，终端输入代表用户的击键，终端输出打印到用户的屏幕或打印机。本章主要讨论这种终端线路用法。异步串行线也用来连接调制解调器做计算机间通信，或连接串行接口打印机。串行接口用于这些目的时，一般只使用系统终端处理能力的一个子集；有些场合则使用特殊处理模块换取更高效率。本章稍后讨论这些替代的终端模块。

4.3BSD UNIX 支持几种终端处理模式。多数时间里终端处于行模式（line mode）即规范模式（cooked mode）：输入字符随键随回显，攒到收到一个回车为止。只有收到回车后，整行才对 shell 或其他从终端行读的进程可用。行模式下用户可以改正打字错误：用擦除字符（erase character）删除最近键入的字符，用字擦除字符（word-erase character）删除最后一个词，用 kill 字符删除当前整行。其他特殊字符产生发给与终端关联的当前进程的信号；这些信号可以中止或暂停处理。另有字符负责启动和停止输出、冲掉输出、或阻止对下一字符做特殊解释。用户可以键入多行输入（上限由实现定义），不必等输入被读取并移出输入队列。用户可以选择这些特殊处理字符，甚至逐个停用。

屏幕编辑器以及与其他计算机通信的程序一般运行在逐字符模式（character-at-a-time mode）下。系统支持两种这样的模式。cbreak 模式下，系统使每个键入的字符一收到即可读取，因而不做 erase 和 kill 处理；其他特殊字符照常处理，信号与流控与行模式无异。另一种逐字符模式是原始模式（raw mode）：所有特殊字符输入处理全部停用，一切字符都传给从终端读的程序。

除处理输入字符外，终端接口驱动还必须对输出做某些处理。多数时候这种处理相当简单：UNIX 的换行转换为回车加换行；接口硬件经编程在输出字符上产生适当的奇偶位。有些终端需要额外时间来处理产生动作的字符（回车、换行、制表），因此终端驱动可以设置成在每个这样的字符后插入延迟。除字符处理外，终端输出例程还必须管理流控——既对用户（用停止和启动字符）也对进程。终端设备与其他计算机外设相比相当慢，向终端写的程序产生输出的速度可能远快于输出送到终端的速度。因此进程填满终端输出队列时必须阻塞，输出排空到一定程度时再重启。


---

# 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.1.-zhong-duan-chu-li-mo-shi.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.
