Linux terminal (TTY)

TTY is an abbreviation or Teletype Teletypewriter, the original teletype means, then such substituent display device and a keyboard gradually. Whether teletype keyboard or monitor, a computer terminal apparatus is present as a so TTY also refers to a computer terminal (terminal) device. To support these TTY devices, Linux implements called TTY subsystem. So TTY refers both to the terminal, also refers to the Linux TTY subsystem, of course, there are more rich TTY (chaos) meaning, this article attempts to explain them. This article demonstrates the use of part of the environment is ubuntu 18.04.

Hardware terminal terminal (TTY)

Early terminal (terminal) is a computer-independence stand of the machine (teletype ie, TTY), probably looks like the following:

It is connected to a computer via a cable terminal, and complete the input and output functions of the computer:

Now virtually extinct physical terminal, all we see are TTY analog video terminal emulation software that is out of the terminal. You can view the system supported by the toe -a command terminal type, not surprisingly, this is a very long list.

Console console

He mentioned terminal without mentioning the console console. The concept of the console and terminal meaning very similar, in fact, we often refer to the same thing with them, but in the early days of the computer, they are really different things.
Some numerical control equipment (such as CNC machine tools) control box, often referred to as the console, by definition, is a direct console control panel of the machine, there are many control buttons above. In the computer, the keyboard and monitor directly connected to the set on the computer is called the console. The terminal is connected through the serial port, not the computer's own equipment, while the console is the device of the computer itself, a computer has only one console. When the computer starts, all the information is displayed on the console, but will not be displayed on the terminal. The same description, the basic device is a computer console, and the terminal device is attached. Computer operating system, not related to terminal information, such as kernel messages, background service messages can be displayed on the console, but will not display on the terminal. For example, when you start and shut down a Linux system, we can see a lot of core information (The figure comes from the vSphere Client "Virtual Machine Console") on the console:

Now terminal and console hardware by the concept, and gradually evolved into the concept of the software. Simply put, you can directly display the message called end system console, others called terminal (console is a terminal). Or we simply do not distinguish between the terminal and the Linux console in the normal use.

The following example is to send a message to the console via / dev / console file, this example we can see a little bit different from the console terminal.
Open the vSphere Client "Virtual Machine Console" (ie, the console), the default display is tty1:

Writes the string "hello world" through other terminal to / dev / console are:
the root eSearch @: ~ # echo "hello world"> / dev / console

String is displayed in the console. Is then switched by Ctrl + Alt + F2 to the terminal console to tty2, again to / dev / console writes the string:
the root eSearch @: ~ # echo "Hello World"> / dev / console

The string tty2 wrote, indicating that Linux always written to / dev / console content will appear in the console of the current virtual terminal (tty1-tty6) years.

TTY equipment

Historically, the beginning is the end terminals, equipped with a printer, keyboard, with a serial port, data transmission to the host through the serial port, and then processed to the host terminal printed. Teletypewriter (teletype) may collectively be viewed as such equipment, the terminal is also referred to simply as (the abbreviation for teletype) TTY.
As shown below (FIG from the Internet):

UART driver
as shown above, the physical terminal is connected to a UART (Universal Asynchronous Receiver and Transmitter) on the computer by a cable. Operating system has a physical driver for the UART transmission management bytes.

Line specification
when the above figure kernel Line discipline (rows specification) is used to provide a buffer editor and some basic editing commands (backspace to remove a single word, clear line, reprint), mainly used to support user input behavior (such as wrong, and need to backspace).

Driving TTY
TTY driver for session management, and the processing of various terminal devices.

UART drivers, line drivers are TTY specification and in the kernel, which is a terminal end of the device, the other end is a user process. Because all devices are files in Linux, so the three of them together are called "TTY device," that is, we often say TTY.

From the terminal emulation software to pseudo-terminal

Then slowly evolved into the terminal keyboard + monitor. If we want the output to a monitor, as long as the content is written to display the corresponding TTY device on it, and then by the TTY layer is responsible for matching the right drive to complete the output, which is the working principle Linux console (The figure comes from the Internet ):

Figure above behavior, the TTY driver and a row previous example specification is similar to, but there is no UART or physical terminal. In contrast, software simulation of a video terminal, and eventually rendered to the VGA monitor. Note that here the emergence of terminal emulation software, which runs in kernel mode . Display and vSphere Client tty1-tty6 "Virtual Machine Console" in all software terminal emulation :

/ Dev / tty1- / dev / tty6 these terminal simulation in a file system representation of the program read and write read and write these files to the terminal emulation.

If we also terminal emulation in user space, the situation will become more flexible, below is xterm and cloning work (The figure comes from the Internet):

To facilitate the user space into a terminal emulator, while still maintaining the TTY subsystem (subsystem means TTY TTY driver and a row specification) intact, the dummy terminal is out of the invention (pseudo terminal or pty). Pseudo-terminal portion of the kernel is divided into two, namely master side and slave side drive implemented in TTY. Note that the above figure the xterm, which is a terminal emulation program running in user mode, such as the Ubuntu Desktop GNOME Terminal:

When creating a pseudo-terminal, creates a device file in / dev / pts directory:

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

终端与伪终端的区别

至此我们可以得出这样的结论:现在所说的终端已经不是硬件终端了,而是软件仿真终端(终端模拟软件)。
关于终端和伪终端,可以简单的理解如下:

  • 真正的硬件终端基本上已经看不到了,现在所说的终端、伪终端都是软件仿真终端(即终端模拟软件)
  • 一些连接了键盘和显示器的系统中,我们可以接触到运行在内核态的软件仿真终端(tty1-tty6)
  • 通过 SSH 等方式建立的连接中使用的都是伪终端
  • 伪终端是运行在用户态的软件仿真终端

总结

通过本文我们可以了解到,真正的硬件终端基本上已经看不到了。在一些连接了键盘和显示器的系统中(当然也包括一些 vsphere 等虚拟环境),我们可以接触到运行在内核态的软件仿真终端。而我们使用最多的则是伪终端。

参考:
解密TTY
Linux TTY/PTS概述
The TTY demystified
What is stored in /dev/pts files and can we open them?
终端、虚拟终端、shell、控制台、tty的区别

Guess you like

Origin www.cnblogs.com/sparkdev/p/11460821.html