openpty() 是做什么的? "the openpty() function finds an available pseudoterminal" 是什么意思?

What does openpty() do? What does it mean that "the openpty() function finds an available pseudoterminal"?

我觉得问这个问题很傻 ("just RTFM!"),但我无法理解这个手册页。 man page blurb 描述 openpty():

The openpty() function finds an available pseudoterminal and returns file descriptors for the master and slave in amaster and aslave. If name is not NULL, the filename of the slave is returned in name. If termp is not NULL, the terminal parameters of the slave will be set to the values in termp. If winp is not NULL, the window size of the slave will be set to the values in winp.

这是什么意思?

我的猜测是 openpty() 试图找到一个对应于进程树中更高层的终端仿真器的伪终端(某个第 nth-parent,即程序是的终端仿真器 运行 中,如果有的话)。然而,文档让它听起来像 openpty() 只是任意地从 /dev/pts.

中抓取一些东西

还有,这里的"available"是什么意思?似乎 "available" 在伪终端的上下文中有多种含义(例如,内核会让终端仿真器创建一个伪终端,伪终端可以是 read/written)。

However, the docs make it sound like openpty() just arbitrarily grabs something out of /dev/pts.

就是这样。

Also, what does "available" mean here? It seems like "available" has multiple meanings in the context of pseudoterminals (e.g. the kernel will let a terminal emulator create a pseudoterminal, a pseudoterminal can be read/written).

可用仅表示当前未被其他进程使用

The openpty() function finds an available pseudoterminal and returns file descriptors for the master and slave in amaster and aslave. If name is not NULL, the filename of the slave is returned in name. If termp is not NULL, the terminal parameters of the slave will be set to the values in termp. If winp is not NULL, the window size of the slave will be set to the values in winp.

这是什么意思?

您将在 return 中收到操作伪术语、其从属部分、主控部分及其文件名所需的所有数据。

伪术语通过实现终端 input/output 语义来模拟 真实终端 。它们在许多情况下使用,以模拟连接的终端(例如,想想当您通过 ssh 登录远程系统时会发生什么)或控制某些交互式程序(设计一个 GUI 终端,其中 shell例如必须运行)。

I feel silly asking this ("just RTFM!")

当然不是,终端和session管理是非常棘手的事情,一开始很难处理。

-----编辑-----

真实终端。它以前如何?在早期(不是很早,恐龙世界更可怕)连接到我们使用终端的系统,一个带有键盘的简单设备,一个屏幕(没有图形)和一些小的硬件和软件来管理一条线被插入其中一台电脑的插头。因此,在 OS 中需要从终端管理所有这些 input/outputs:在需要时将键入的字符回显到屏幕,缓冲 input/output 以便能够在命令行上删除字符等。OS 部分是通过表示为设备的终端驱动程序管理的,例如 /dev/ttyXXX。一台这样的物理设备,一台OS终端设备。

现在这样的物理设备已经不存在了,除了一些特殊情况,比如服务器控制台(我的意思是直接连接到机器的那个!,或者在启动时你的屏幕上所有这些奇怪的消息都以文本形式出现模式等)。我们现在通过许多不同的 virtual 连接到 OSes(今天的计算机世界是关于 virtuality),比如模拟 GUI 终端物理终端的行为。然后,旧物理线路的两端部分在一个伪终端中表示,伪终端分为两部分:主站和从站。 slave 表示应用程序看到的 OS 中的旧部分。 master 代表旧物理线路的插头,因此它现在作为软件提供,让您编写一个能够 input/output 控制 input/output 的应用程序 虚拟线路并且表现得像真实终端