> 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/man/man4/pts.4.md).

# pts.4

`pts` — 伪终端驱动程序

## 名称

`pts`

## 描述

`pts` 驱动程序为称为 *伪终端* 的设备对提供支持。伪终端是一对字符设备，一个 *主* 设备和一个 *从* 设备。从设备为进程提供的接口与 [tty(4)](/man/man4/tty.4.md) 中所述相同。然而，所有其他提供 [tty(4)](/man/man4/tty.4.md) 中所述接口的设备背后都有某种硬件设备，而从设备则通过伪终端的主端由另一个进程操控。也就是说，写入主设备的任何内容都会作为输入提供给从设备，写入从设备的任何内容也会作为输入呈现在主设备上。

以下 ioctl(2) 调用仅适用于伪终端：

**`TIOCPKT_FLUSHREAD`** 当终端的读队列被刷新时。

**`TIOCPKT_FLUSHWRITE`** 当终端的写队列被刷新时。

**`TIOCPKT_STOP`** 当终端输出被停止时（如同 `^S`）。

**`TIOCPKT_START`** 当终端输出被重新启动时。

**`TIOCPKT_DOSTOP`** 当 `VSTOP` 为 `^S` 且 `VSTART` 为 `^Q` 时。

**`TIOCPKT_NOSTOP`** 当启停字符不是 `^S/^Q` 时。

**`TIOCPKT`** 启用/禁用 *packet* 模式。通过（以引用方式）指定非零参数启用 packet 模式，通过指定零参数禁用。当应用于伪终端的主端时，随后对终端的每次 read(2) 都会返回写入伪终端从端的数据，并在前面加一个零字节（符号定义为 `TIOCPKT_DATA`），或返回一个反映控制状态信息的单字节。在后一种情况下，该字节是以下零个或多个位的包含或：在此模式使用期间，可通过 select(2) 的异常条件检测主端是否有待读的控制状态信息。此模式由 rlogin(1) 和 rlogind(8) 使用，用于实现远程回显、本地 `^S/^Q` 流控的远程登录，并正确反向刷新输出；也可被其他类似程序使用。

**`TIOCGPTN`** 获取设备单元号，可用于生成伪终端从设备的文件名。此 ioctl(2) 不应直接使用，应使用 ptsname(3) 函数。

**`TIOCPTMASTER`** 判断文件描述符是否指向伪终端主设备。此 ioctl(2) 不应直接使用，用于实现 grantpt(3) 等例程。

## 文件

此伪终端实现所使用的文件为：

**`/dev/pts/[num]`** 伪终端从设备。

## 诊断

无。

## 参见

posix\_openpt(2), grantpt(3), ptsname(3), [pty(4)](/man/man4/pty.4.md), [tty(4)](/man/man4/tty.4.md)

## 历史

伪终端驱动程序出现于 4.2BSD。在 FreeBSD 8.0 中，它被 `pts` 驱动程序取代。


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man4/pts.4.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
