> 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.4.-tty-jie-gou.md).

# 9.4 tty 结构

每个终端硬件驱动都有一个数据结构，存放它支持的每条线路的状态。这个结构即 tty 结构（`tty` 结构），含状态信息、输入和输出队列、前述 `ioctl` 操作设置的模式和选项，以及行规程号（见表 9.2）。`tty` 结构由硬件驱动和行规程共享：对行规程的调用都要求以 `tty` 结构为参数，驱动按次设备号找到正确的 `tty`。该结构还含有行规程所需的设备驱动信息。

表 9.2 tty 结构

| 类型   | 描述       |
| ---- | -------- |
| 字符队列 | 裸输入队列    |
|      | 规范化输入队列  |
|      | 设备输出队列   |
| 硬件参数 | 设备号      |
|      | 输出过程     |
|      | 终端列号     |
| 选择   | 选择读的进程   |
|      | 选择写的进程   |
| 状态   | 进程组      |
|      | 标志       |
|      | 行规程      |
|      | 行数与列数    |
|      | 输入和输出波特率 |
|      | 输入特殊字符   |

`tty` 结构的各部分包括：

* 硬件终端线路的状态信息。`t_state` 字段含线路状态（打开、有载波或等载波）和主要文件选项（使用非阻塞 I/O 或信号驱动 I/O）。流控、延迟处理和同步的瞬态也存放在这里。
* 输入和输出队列。硬件驱动发送输出队列中的字符。行规程一般用 `t_rawq` 和 `t_canq`（规范化队列）装输入：行模式下规范化队列装整行，裸队列装当前未完的半行。不用 `t_rawq` 和 `t_canq` 的行规程有时用另一种形式的接收缓冲覆盖这一区域。
* 硬件与软件模式和参数。`t_flags` 字含 TIOCSETP 和 TIOCLSET 设置的模式；TIOCSETP 设置的线路速度放进 `t_ispeed` 和 `t_ospeed`。
* 行规程号（`t_line`）。
* 特殊字符。需要特殊动作的字符都存在 `t_tchars`：这些字符由 TIOCSETP（erase 和 kill）、TIOCSETC（中断、退出、启动、停止、文件结束和替用行结束）以及 TIOCSLTC（挂起、延迟挂起、重印行、冲掉输出、字擦除和转义下一字符）设置。
* 硬件驱动信息。包括 `t_oproc`——数据放上输出队列后启动传输的驱动过程；`t_addr`——硬件相关数据结构或寄存器的地址；`t_dev`——终端线路的设备号。
* 终端行规程的软件状态。包括终端列号以及制表与擦除处理的计数（`t_col`、`t_rocount`、`t_rocol`）、终端的进程组（`t_pgrp`），以及选择输入或输出的进程的身份（`t_rsel` 和 `t_wsel`）。
* 终端或窗口大小（`t_winsize`）。内核不使用这一信息，把它存在这里是为了呈现一致的接口。此外 4.3BSD 提供 SIGWINCH 信号（源自 Sun Microsystems 的 SunOS），窗口大小改变时可以发送。这对 X Window System \[Scheifler & Gettys, 1986] 等允许用户动态改变窗口大小的窗口软件包很有用：在这样的窗口里运行的文本编辑器等程序需要被告知情况有变、应重新检查窗口大小。

`tty` 结构由硬件终端驱动的 open 例程和行规程的 open 例程初始化。


---

# 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.4.-tty-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.
