Talking about the Linux tty system, clarifying the tty driver level and various concepts

Although the Linux kernel is written in C language, it embodies object-oriented design ideas everywhere. For many friends who only know C language, it is difficult to understand, especially the tty system, which involves many confusing concepts.

1. tty, terminal and console in ancient times

In ancient times, the three concepts of tty, terminal and consle corresponded to various hardware

console: console

On early computers, there was often a panel with a large number of switches and indicators, which could perform some low-level operations on the computer. This panel is called Console. A computer usually has only one Console, which is often a part of the host computer and shares a cabinet with the CPU .

terminal: terminal

The console can realize complete control of the computer and is used to manage the computer, but it is inconvenient to serve users. Later, with the development of computer technology and the continuous improvement of computing power, multi-user operating systems (especially UNIX) appeared, allowing each user to connect to the host through a terminal device (Terminal). The operating system administrator assigns an account to each user, "login "When the system obtains the right to use the computer, the computer is considered to have realized the service purpose.

A terminal means a device that can be used to interact with a computer—usually a monitor and keyboard.

The relationship between console and terminal: console is on the host, there is only one, and the terminal is connected to the host, there can be many

 tty:teletypewriter

Literally teletypewriter. It is a kind of terminal. Linux uses this name in the kernel (explained later).

Interrupt Shell

Some beginners may mistake the shell for a terminal.

Shell is one of the most important applications in the UNIX/Linux system , responsible for interpreting and executing user instructions, printing results, and interacting with users.

The terminal itself does not execute the commands entered by the user, it is only responsible for transmitting the input content to the host system (input devices such as keyboards), and presenting the results returned by the host system to the user (printing paper already on the screen). It is the Shell that is responsible for interpreting and executing the commands entered by the user and returning the results! It is an intermediate bridge between users and the system kernel.

A shell in a broad sense can be a graphical interface or a command line interface.

2. tty abstraction

With the development, the concepts of terminal, tty, and console began to be confused .

Teletypewriters have been basically eliminated, consoles and terminals are no longer independent peripherals, and keyboards and monitors on PCs are integrated into consoles/terminals.

There are not only teletypes, but also many forms with screens.

In order to shield these differences, Linux introduced the tty driver layer:

Hardware driver-application program becomes hardware driver-tty driver-application program , which is a common separation idea in driver involvement, abstracting commonality. That is, the original hardware driver is divided into two parts.

Interaction between the terminal and the kernel:

Here comes the real point: how does the terminal interact with the program?

For example: we enter the backspace key, which itself is just a value, how do we know that the function is backspace?

Linux introduced a line discipline (line discipline), which is equivalent to a filter, or an interpreter.

This could of course be achieved by the application itself, but according to the UNIX design "philosophy" the application should be kept as simple as possible.

For convenience, the operating system provides an editing buffer and some basic editing commands (backspace, clear single word, clear line, reprint), which are enabled by default within the line discipline (line discipline ) .

Applications can disable these features by setting the row specification to raw mode ( raw  mode) instead of the default cooked or canonical mode ( cooked  and  canonical ), that is, get the raw value.

Most interactive programs (editors, mail clients, shells, and all dependencies curses) readlinerun in raw mode and process all line editing commands themselves. Line specifications also include options for automatic conversion between character echo and carriage return and line feed (Translator's Note: \r\n and  ).\n

That is, the tty in the hardware driver-tty-application is split into line discipline and tty driver

That is: hardware driver-line discipline (line discipline)-tty driver-application

Here is a place that is easy to confuse. The tty driver is not a real driver, but just abstracts some interfaces. For the application , it is a driver. For the application, the underlying differences are shielded, which is equivalent to :

tty hardware - tty hardware driver - application

So in fact, he is not called driver, but generally called tty core core

So this statement is more clear: hardware - hardware drvier - line discipline line discipline - tty core - application

We know that ancient teletypewriters are connected to the host through uart.

People input through a typewriter (terminal), the uart is connected to the host, and through the line discipline (line discipline), it reaches the tty, and the program reads the tty.

The triplet of UART driver, line procedure and TTY driver can be called a TTY device, which is what we often call TTY.

In fact, this corresponds to our current tty driver, line procedures, and tty core three parts, which together form a tty device.

3. Terminal virtualization

Virtual Terminal

A PC usually has only one set of keyboard and monitor, that is, only one set of physical terminal devices. The Linux kernel maps this set of keyboards and monitors to 6 character terminal device files, namely /dev/tty1~tty6, called " virtual Terminal (Virtual Terminal) ", which can be switched by Ctrl-Alt-F1~F6.

/dev/tty0 points to the virtual terminal that the user is currently using . If the user switches to /dev/tty4, then /dev/tty0 points to /dev/tty4.

Serial Port Terminal

Later, the computer developed a serial port (Serial Port). At that time, the biggest use of the serial port was to connect the terminal. The computer regarded the peripheral device connected to the serial port as a character device , and the peripheral device was called " Serial Port Terminal" . The serial port corresponds to the device /dev/ttyS1, /dev/ttyS2...etc. in the Linux system.

The virtual terminal above is actually a real terminal, but it is 1 to 6. Here comes the real virtual terminal:

Terminal Emulator

The graphical interface program built based on the existing keyboard and display drivers in the system has only one fundamental purpose - to simulate the function of a real terminal. For example, we connect to the linux host through the MobaXterm software on windows, and MobaXterm is a terminal emulator. There is also a built-in terminal emulator in the Linux desktop version system.

Why is the terminal emulator called an emulator? Because the real terminal is a full-screen black one without a window (press ctrl+alt+f1), with a window here, which is based on the X window system of linux Simulated terminal equipment.

Pseudo terminal: pseudo terminal

Pseudo terminal (pseudo tty, abbreviated as pty) is a pair of two-way interconnected devices in the UNIX/Linux kernel, divided into master device (pty master) and slave device (pty slave), also known as "pseudo terminal pair (pty pair) )".

Terminal emulator (terminal emulator) is a module that runs in the kernel, and we can also let the terminal emulator run in the user area. The terminal emulation program running in the user area is called a pseudo terminal (pseudo terminal, PTY).

We press and hold ctrl+alt+t in the Linux system to open a terminal

The terminal emulator simulates a terminal, and this terminal  interacts with the program through the pseudo terminal: the pseudo terminal, the line procedure , and the tty .

Why is the concept of pseudo-terminal proposed in Linux? Can't command-line programs such as shell read data directly from the monitor and keyboard?

Yes, but that is a real tty, but we only have one screen, what if we want to open multiple terminals.

If it is a remote login, although the terminal is connected to the host through the socket interface based on the TCP/IP protocol, the standard input, standard output, and standard error of the program on the remote host cannot be associated with the socket. (The results of the program are still displayed locally, unless the return is implemented in the program, which increases the complexity of the program)

In order to run multiple terminal emulators on the same screen and realize remote login, there is a pseudo-terminal, which realizes multiplexing and hardware that is separated from the host.

Pseudo-terminals are essentially a pair of character devices created by a terminal emulator running in user mode. The master device is named /dev/ptmx, and the slave devices are named /dev/pts0, /dev/pts1.../dev/ptsN, etc.

/dev/ptmx is a character device file. When the process opens the /dev/ptmx file, the process will simultaneously obtain a file descriptor pointing to the pseudoterminal master (ptm) and a pseudoterminal slave (pts) created in the /dev/pts directory. ) equipment. (Like both ends of a pipe)

Each file descriptor obtained by opening the /dev/ptmx file is an independent ptm, which has its own associated pts, and ptmx (it can be considered that there is a ptmx object in memory) will maintain the file descriptor and pts internally. Corresponding relationship, reading and writing to this file descriptor will be forwarded to the corresponding pts by ptmx.

We can view the file descriptors opened by ptmx through the lsof command:

$ sudo lsof /dev/ptmx

induction:

Terminals are divided into real terminals (tty1-6, ttySn, etc.) and pseudo terminals

The tty1-6 in the real terminal is also called a virtual terminal, because there is actually only one set of input and output.

console: the terminal used to display system messages, the terminal can be a console

The tty driver was originally used to abstract the tty hardware device, and later developed into a line procedure + tty core. The term tty driver is used to indicate the hardware part driver

Reference article:

Decrypt TTY - QiuhaoLi - 博客园

Guess you like

Origin blog.csdn.net/freestep96/article/details/127338674