Linux 终端(TTY)

TTY 是 Teletype 或 Teletypewriter 的缩写,原来是指电传打字机,后来这种设备逐渐键盘和显示器取代。不管是电传打字机还是键盘显示器,都是作为计算机的终端设备存在的,所以 TTY 也泛指计算机的终端(terminal)设备。为了支持这些 TTY 设备,Linux 实现了一个叫做 TTY 的子系统。所以 TTY 既指终端,也指 Linux 的 TTY 子系统,当然 TTY 还有更丰富(混乱)的含义,本文试图把它们解释清楚。本文中演示部分使用的环境为 ubuntu 18.04。

早期的终端(terminal) 是一台独立于计算机的机器(teletype 即, TTY),大概像下面的样子:

其端子通过电缆与计算机相连,完成计算机的输入输出功能:

[En]

Its terminal is connected with the computer through the cable, and completes the input and output function of the computer:

现在物理终端实际上已经绝迹了,我们看到的所有TTY都是模拟视频终端,也就是软件模拟的终端。您可以通过toe-a命令检查系统支持的终端类型,这并不奇怪,这是一个很长的列表。

[En]

Now the physical terminal is actually extinct, and all the TTY we see are analog video terminals, that is, terminals emulated by software. You can check the types of terminals supported by the system through the toe-a command, which, not surprisingly, is a long list.

当谈到终端时,你不得不提到控制台。控制台的概念和终端的含义非常相似,其实我们现在经常用它们来代表一样的东西,但在计算机早期,它们真的是不同的东西。

[En]

When it comes to terminals, you have to mention the console console. The concept of the console is very similar to the meaning of the terminal, in fact, we often use them to represent the same thing now, but in the early days of computers, they were really different things.

一些数控设备(比如数控机床)的控制箱,通常会被称为控制台,顾名思义,控制台就是一个直接控制设备的面板,上面有很多控制按钮。 在计算机里,把那套直接连接在电脑上的键盘和显示器就叫做控制台。而终端是通过串口连接上的,不是计算机自身的设备,而控制台是计算机本身就有的设备,一个计算机只有一个控制台。 计算机启动的时候,所有的信息都会显示到控制台上,而不会显示到终端上。这同样说明,控制台是计算机的基本设备,而终端是附加设备。计算机操作系统中,与终端不相关的信息,比如内核消息,后台服务消息,都可以显示到控制台上,但不会显示到终端上。比如在启动和关闭 Linux 系统时,我们可以在控制台上看到很多的内核信息(下图来自 vSphere Client 中的 “Virtual Machine Console”):

现在,终端和控制台都已经从硬件的概念演变为软件的概念。简单来说,能直接展示系统消息的终端叫控制台,另一个叫终端(控制台也是终端)。或者我们在Linux中根本不区分终端和控制台。

[En]

*Now both terminal and console have evolved from the concept of hardware to the concept of software. To put it simply, the terminal that can directly display the system message is called the console, and the other is called the terminal (the console is also a terminal). Or we don’t distinguish between the terminal and the console in Linux at all. *

以下示例通过/dev/控制台文件向控制台发送消息。在本例中,我们可以看到控制台和终端之间的细微差别。

[En]

The following example is sending a message to the console through the / dev/console file. In this example, we can see the slight difference between the console and the terminal.

打开 vSphere Client 中的 “Virtual Machine Console”(即控制台),默认显示的是 tty1:

通过其他终端将字符串“Hello world”写入/dev/console:

[En]

Write the string “hello world” to / dev/console through other terminals:

root@esearch:~# echo “hello world” > /dev/console

该字符串将显示在控制台中。然后通过Ctrl+Alt+F2将控制台中的终端切换到tty2,并再次向/dev/控制台写入一个字符串:

[En]

The string is displayed in the console. Then switch the terminal in the console to tty2 through Ctrl + Alt + F2 and write a string to / dev/console again:

root@esearch:~# echo “hello world” > /dev/console

这次,字符串被写入tty2,这意味着Linux始终将/dev/控制台的内容写入控制台的当前虚拟终端(tty1-tty6)中。

[En]

This time the string is written to the tty2, which means that the Linux always writes the contents of / dev/console to be displayed in the current virtual terminal (tty1-tty6) in the console.

在历史上,终端最初是一个终端,配备了打印机、键盘,带有串口,通过串口将数据传输到主机,然后主机进行处理并移交给终端进行打印。电传打字机可以看作是这类设备的总称,因此终端也被称为TTY(电传打字的缩写)。

[En]

Historically, the terminal is a terminal at the beginning, equipped with a printer, keyboard, with a serial port, through which data is transmitted to the host, and then the host is processed and handed over to the terminal to print. Teletypewriter (teletype) can be regarded as the general name of this kind of equipment, so the terminal is also referred to as TTY (abbreviation of teletype).

如下图所示(下图来自互联网):

UART 驱动
如上图所示,物理终端通过电缆连接到计算机上的UART(通用异步收发机)。操作系统中有一个UART驱动程序,用于管理字节的物理传输。

[En]

As shown in the figure above, the physical terminal is connected to the UART (universal asynchronous receiver and transmitter) on the computer through a cable. There is a UART driver in the operating system that manages the physical transfer of bytes.

行规范
上图内核中的Line规程(Line规范)用于提供编辑缓冲区和一些基本的编辑命令(退格键、清除单字、清除行号、重印),主要用于支持用户在打字时的行为(如打字错误、需要退格)。

[En]

The Line discipline (line specification) in the kernel in the image above is used to provide an editing buffer and some basic editing commands (backspace, clear individual words, clear lines, reprint), mainly used to support the user’s behavior when typing (such as typing wrong, need to backspace).

TTY 驱动
TTY 驱动用来进行会话管理,并且处理各种终端设备。

UART 驱动、行规范和 TTY 驱动都位于内核中,它们的一端是终端设备,另一端是用户进程。因为在 Linux 下所有的设备都是文件,所以它们三个加在一起被称为 “TTY 设备”,即我们常说的 TTY。

后来,终端逐渐演变为键盘+显示器。如果我们想要将内容输出到显示器上,只需将内容写入显示器相应的TTY设备,然后TTY层负责匹配相应的驱动程序来完成输出。这也是Linux控制台的工作方式(下图来自互联网):

[En]

Later, the terminal gradually evolved into a keyboard + monitor. If we want to output the content to the monitor, just write the content to the corresponding TTY device of the display, and then the TTY layer is responsible for matching the appropriate driver to complete the output. This is also how the Linux console works (the following figure is from the Internet):

上图中,TTY 驱动和行规范的行为与前面的示例类似,但不再有 UART 或物理终端。相反,软件仿真出视频终端,并最终被渲染到 VGA 显示器。注意,这里出现了软件仿真终端,它们是运行在内核态的。显示器和 vSphere Client “Virtual Machine Console” 中的 tty1-tty6 都是 软件仿真终端

/dev/tty1-/dev/tty6 是这些仿真终端在文件系统中的表示,程序通过对这些文件的读写实现对仿真终端的读写。

如果我们也在用户空间做终端仿真,情况会变得更灵活。下图显示了xTerm及其克隆的工作原理(下图来自互联网):

[En]

If we also do terminal emulation in user space, the situation will become more flexible. The following figure shows how xterm and its clones work (the following figure is from the Internet):

为了便于终端仿真进入用户空间,同时保持TTY子系统(TTY子系统指的是TTY驱动器和行规范)的完整性,发明了伪终端(伪终端或PTY)。伪终端在内核中分为两部分,即在TTY驱动程序中实现的主端和从端。注意上图中的xTerm,这是一个运行在用户模式下的终端仿真程序,例如Ubuntu Desktop中的GNOME终端:

[En]

In order to facilitate terminal emulation into user space, while still keeping the integrity of the TTY subsystem (TTY subsystem refers to TTY driver and row specification), pseudo terminals are invented (pseudo terminal or pty). The pseudo terminal is divided into two parts in the kernel, namely, master side and slave side implemented in the TTY driver. Notice xterm in the figure above, which is a terminal emulation program running in user mode, such as GNOME Terminal in Ubuntu Desktop:

创建伪终端时,会在/dev/pt目录中创建一个设备文件:

[En]

When you create a pseudo terminal, a device file is created in the / dev/pts directory:

如果是通过 PuTTY 等终端仿真程序通过 SSH 的方式远程连接 Linux,那么终端仿真程序通过 SSH 与 PTY master side 交换数据。

至此,我们可以得出结论,我们所说的终端不再是硬件终端,而是软件仿真终端(终端仿真软件)。

[En]

At this point, we can come to the conclusion that the terminal we are talking about is no longer a hardware terminal, but a software simulation terminal (terminal simulation software).

关于终端和伪终端,可以简单理解为:

[En]

With regard to terminals and pseudo terminals, it can be simply understood as follows:

  • 实打实的硬件终端基本不见踪影现在终端和伪终端是软件仿真终端(即终端仿真软件)。
    [En]

    the real hardware terminals are basically out of sight. Now the terminals and pseudo terminals are software emulation terminals (that is, terminal emulation software).*

  • 在某些连接到键盘和显示器的系统中,我们可以访问在内核模式下运行的软件仿真终端(tty1-tty6)。
    [En]

    in some systems connected to keyboards and monitors, we have access to software emulation terminals (tty1-tty6) running in kernel mode.*

  • 通过 SSH 等方式建立的连接中使用的都是伪终端
  • 伪终端是在用户模式下运行的软件仿真终端。
    [En]

    pseudo terminal is a software emulation terminal running in user mode.*

通过这篇文章,我们可以了解到,真正的硬件终端基本上是看不见的。在一些连接到键盘和显示器的系统中(当然包括一些虚拟环境,如vSphere),我们可以访问在内核状态下运行的软件仿真终端。而我们使用最多的是伪终端。

[En]

Through this article, we can learn that the real hardware terminal is basically out of sight. In some systems connected to keyboards and monitors (including some virtual environments such as vsphere, of course), we have access to software emulation terminals running in the kernel state. And the ones we use most are pseudo terminals.

Original: https://www.cnblogs.com/sparkdev/p/11460821.html
Author: sparkdev
Title: Linux 终端(TTY)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/7235/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部