dev_open linux,linux 伪终端设备 /dev/ptmx

时间:2025-01-21 13:19:50

伪终端(pseudoterminal): ptmx, pts (伪终端master和伪终端slave).

/dev/ptmx用于创建伪终端主从设备对.当我们用open打开/dev/ptmx设备后, 返回主设备的文件描述符,

并且在/dev/pts/目录下创建一个伪终端从设备.

在成功打开/dev/ptmx后, 可以通过函数ptsname()来获取从设备的path.

想要打开open伪终端从设备,必须先使用主设备的文件描述符调用grantpt(), unlockpt().

当主设备和从设备都打开后, 在主设备写入的数据可以在从设备上读取, 在从设备上写入的数据可以在主设备读取.

Data written to the slave is presented on the master descriptor as input.  Data written to the master is presented to the slave as input.

伪终端应用:

当需要调用调用exece等函数执行系统命令时,

通过fork函数创建子进程, 在子进程中调用exece执行系统命令, 并且把子进程的stdout和stderr通过dup2重定向

到伪终端的从设备;而在父进程中通过读取伪终端的主设备来获取子进程支持系统命令的输出信息.

详细处理,请看android 中的函数android_fork_execvp_ext()

阅读(4434) | 评论(0) | 转发(0) |