伪终端提供了网络连接到面向终端程序之间那缺失的一环。伪终端是一对互联的虚拟设备:主伪终端和从伪终端,有时会被称为伪终端对。伪终端对提供了一条IPC通道,两个进程能分别打开主端和从端,并通过伪终端双向传输数据。
关于伪终端,关键点在于从设备表现得就像一个标准终端一样。所有可以施加于终端设备的操作同样也可以施加于伪终端从设备上
典型的情况下,两个程序利用伪终端的逻辑如下:面向终端程序的标准输入、输出以及错误都连接到伪终端设备上,它也是程序的控制终端。在终端的另一侧,驱动程序作为用户的代理,提供面向终端程序的输入,并读取程序的输出
相关api如下:
点击(此处)折叠或打开
- #define _XOPEN_SOURCE 600
- #include <stdlib.h>
- #include <fcntl.h>
- int posix_openpt(int flags);
- /*returns file descriptor on success, or -1 on error*/
点击(此处)折叠或打开
- #define _XOPEN_SOURCE 500
- #include <stdlib.h>
- int grantpt(int mfd);
- /*returns 0 on success, or -1 on error*/
解锁从设备:
点击(此处)折叠或打开
- #define _XOPEN_SOURCE 500
- #include <stdlib.h>
- int unlockpt(int mfd);
- /*returns 0 on success, or -1 on error*/