[Linux] terminals relations

1. Overview

tty may be essentially divided into a console (/ dev / console), the virtual terminal (/ dev / tty0 ~ n) and the pseudo-terminal (/ dev / pts / 0 ~ n).

There is a certain relationship between them.

To illustrate this relationship, first prepare two terminals:

1.console: the terminal is turned on, the conventional system startup information, are output to the upper terminal, directly connected to it, a display connected to the host card, it is actually output on the system bus, to the terminal hereinafter referred to unity as tty_a;

2. A pseudo-terminal remote login, the terminal is referred to below unity tty_b;

 

2. Example
1. System Console: / dev / console

A computer is only one console, which is output to the terminal on the video card, so any method to write to / dev / console, the terminal will be output to boot.

tty_b type:

[tty_b] # echo "Hello.I'm tty_b" > /dev/console

tty_a output:

[tty_a] # Hello.I'm tty_b

 

2. Current Console: / dev / tty

This is the concept of the application, if the current process has a control terminal (Controlling Terminal), then the / dev / tty device file is the current process console. For your login shell, / dev / tty is the console, the device number you are using (5,0). But it does not mean the console on any physical meaning, / dev / tty is mapped to the current device (using the command "tty" can view it specifically corresponds to which actual physical console device). Output to / dev / tty will only display the contents of the current work on the terminal (either login or in ttyn in the pty). If you then dev / tty is mapped in the console interface (that is, characters interface) to a between dev / tty1-6 (depending on your current console number), but if you are now in a graphical interface (Xwindows ), then you will find now / dev / tty is mapped to the pseudo terminal / dev / pts of. / Dev / tty somewhat similar to a coupling terminal device actually used.

tty_a type:

[tty_a] # echo "Hello.I'm tty_a" > /dev/tty

tty_a output:

[tty_a] # Hello.I'm tty_a

 

3. Virtual Console: / dev / ttyn

/ dev / ttyn is the process virtual console, they share the same real physical console .

If you open one of these files in the process and that the file is not in the other processes of the console, and that the file is to console the process. Printf outputs data to process here. On the PC, the user can use the alt + Fn switching console, look and feel the presence of a plurality of screens, which correspond to the virtual console tty1 ~ n, wherein:

/ Dev / tty1 and other representatives of the first virtual console

For example, when using ALT + F2 is switched virtual console system of / dev / tty2, the current console (/ dev / tty) points to / dev / tty2

在UNIX系统中,计算机显示器通常被称为控制台(Console)。它仿真了类型为Linux的一种终端,并且有一些设备特殊文件与之相关联:tty0、tty1、tty2等。当你在控制台上登录时,使用的是tty1。使用Alt+[F1—F6]组合键时,我们就可以切换到tty2、tty3等上面去。

你可以登录到不同的虚拟控制台上去,因而可以让系统同时有几个不同的会话存在。

而比较特殊的是/dev/tty0,他代表当前虚拟控制台,是当前所使用虚拟控制台的一个别名。因此不管当前正在使用哪个虚拟控制台(注意:这里是虚拟控制台,不包括伪终端),系统信息都会发送到/dev/tty0上。只有系统或超级用户root可以向/dev/tty0进行写操作。tty0是系统自动打开的,但不用于用户登录。在Framebuffer设备没有启用的系统中,可以使用/dev/tty0访问显卡。

所以向/dev/tty0写入即等同于向/dev/console写入。

tty_b中输入:

[tty_b] # echo "Hello.I'm tty_b" > /dev/tty0

tty_a中输出:

[tty_a] # Hello.I'm tty_b

 

4. 伪终端:pty(pseudo-tty)

伪终端(Pseudo Terminal)是终端的发展,为满足现在需求(比如网络登陆、xwindow窗口的管理)。它是成对出现的逻辑终端设备(即master和slave设备, 对master的操作会反映到slave上)。它多用于模拟终端程序,是远程登陆(telnet、ssh、xterm等)后创建的控制台设备。

历史上,有两套伪终端软件接口:

BSD接口:较简单,master为/dev/pty[p-za-e][0-9a-f] ;slave为 /dev/tty[p-za-e][0-9a-f] ,它们都是配对的出现的。例如/dev/ptyp3和/dev/ttyp3。但由于在编程时要找到一个合适的终端需要逐个尝试,所以逐渐被放弃。

Unix 98接口:使用一个/dev/ptmx作为master设备,在每次打开操作时会得到一个master设备fd,并在/dev/pts/目录下得到一个slave设备(如 /dev/pts/3和/dev/ptmx),这样就避免了逐个尝试的麻烦。由于可能有好几千个用户登陆,所以/dev/pts/*是动态生成的,不象其他设备文件是构建系统时就已经产生的硬盘节点(如果未使用devfs、udev、mdev等) 。第一个用户登陆,设备文件为/dev/pts/0,第二个为/dev/pts/1,以此类推。它们并不与实际物理设备直接相关。现在大多数系统是通过此接口实现pty。

我们在X Window下打开的终端或使用telnet 或ssh等方式登录Linux主机,此时均通过pty设备。例如,如果某人在网上使用telnet程序连接到你的计算机上,则telnet程序就可能会打开/dev/ptmx设备获取一个fd。此时一个getty程序就应该运行在对应的/dev/pts/*上。当telnet从远端获取了一个字符时,该字符就会通过ptmx、pts/*传递给 getty程序,而getty程序就会通过pts/*、ptmx和telnet程序往网络上返回“login:”字符串信息。这样,登录程序与telnet程序就通过“伪终端”进行通信。

telnet/dev/ptmx(master)pts/*(slave)getty

如果一个程序把 pts/*看作是一个串行端口设备,则它对该端口的读/写操作会反映在该逻辑终端设备对的另一个/dev/ptmx上,而/dev/ptmx则是另一个程序用于读写操作的逻辑设备。这样,两个程序就可以通过这种逻辑设备进行互相交流,这很象是逻辑设备对之间的管道操作。对于pts/*,任何设计成使用一个串行端口设备的程序都可以使用该逻辑设备。但对于使用/dev/ptmx的程序,则需要专门设计来使用/dev/ptmx逻辑设备。

通过使用适当的软件,就可以把两个甚至多个伪终端设备连接到同一个物理串行端口上。

 

3. 关系图

 

Guess you like

Origin www.cnblogs.com/yiyide266/p/11601036.html