关于tty驱动的简单问题

原创文章,转载请注明出处.转载自: Li Haifeng's Blog
本文链接地址: 关于tty驱动的简单问题

~/temp/linux-3.0.8$ cat /proc/tty/drivers
/dev/tty             /dev/tty        5       0 system:/dev/tty
/dev/console         /dev/console    5       1 system:console
/dev/ptmx            /dev/ptmx       5       2 system
/dev/vc/0            /dev/vc/0       4       0 system:vtmaster
rfcomm               /dev/rfcomm   216 0-255 serial
ttyprintk            /dev/ttyprintk   5       3 console
serial               /dev/ttyS       4 64-111 serial
pty_slave            /dev/pts      136 0-1048575 pty:slave
pty_master           /dev/ptm      128 0-1048575 pty:master
unknown              /dev/tty        4 1-63 console
根据LDD p547:
To determine what kind of tty drivers are currently loaded in the kernel and what tty
devices are currently present, look at the /proc/tty/drivers file. This file consists of a
list of the different tty drivers currently present, showing the name of the driver, the
default node name, the major number for the driver, the range of minors used by the
driver, and the type of the tty driver.

问题:

根据LDD的描述,所有在/tty/drivers中的驱动都存在对应的设备。可是,我在本机并没有发现有/dev/ptm设备。

之后,看了linux-3.0.8的source code: drivers/tty/pty.c 中的unix98_pty_init函数:发现ptm和pts是一起注册的。

1. 为什么有/dev/pts,而/dev/ptm没有呢?

2. 另外,为什么要有个pty:slave 和pty:master呢?

答案(两行黑体字分别回答了问题1和问题2):

伪终端用于创建登陆会话或提供其它功能,比如通过 TTY line discipline (包括SLIP或者PPP功能)来处理任意的数据生成。每一个 PTY 都有一个master端和一个slave端。按照 System V/Unix98 的 PTY 命名方案,所有master端共享同一个 /dev/ptmx 设备节点(打开它内核将自动给出一个未分配的PTY),所有slave端都位于 /dev/pts 目录下,名为 /dev/pts/# (内核会根据需要自动生成和删除它们)。 一旦master端被打开,相应的slave设备就可以按照与 TTY 设备完全相同的方式使用。master设备与slave设备之间通过内核进行连接,等价于拥有 TTY 功能的双向管道(pipe)。

wiki给出的答案:
BSD PTYs have been made obsolete by Unix98 ptys, which allow race-free access and their number is not limited by nomenclature, only by implementation.
/dev/ptmx is the "pseudo-terminal master multiplexer" which, when opened, causes a slave node /dev/pts/N node to appear (with N being an integer). ptsname(3) can be used to obtain the name of the slave node. The file descriptor, obtained by opening ptmx plus the slave node, form the tty pair.

From Li Haifeng's Blog, post 关于tty驱动的简单问题

Post Footer automatically generated by wp-posturl plugin for wordpress.

分享到: