Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between TTY/PTS in Linux?

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what is the difference between TTY/PTS in Linux, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Overview of Linux TTY/PTS

When we type a letter on the keyboard, how exactly do we send it to the corresponding process? What are the functions and differences of outputs like tty1 and pts/0 that we see through ps, who, and so on?

Before the advent of TTY historically multitasking computers

Before computers came out, people were already using a device called teletype, which was used to send messages to each other, something like this:

+-+ Physical Line +-+ | teletype | | teletype | +-+-+

The two teletype are connected by a wire, and there may also be devices like modems at both ends of the line (they will be ignored here). When you hit the keyboard on one end of the teletype, the corresponding data will be sent to the teletype on the other end. I don't know exactly what the function is. (I thought in my head that the picture was typed at one end and printed at the other end.)

These are all antiques and have not been touched at all, so we can only speculate simply.

After the advent of computers that support multitasking

When the computer supports multitasking, people think of connecting these teletype to the computer as the terminal of the computer, so that they can operate the computer.

There are two main reasons for using teletype (personal opinion):

In reality, there are already a large number of teletype from different vendors, which can make full use of existing resources.

The related network of teletype has been relatively mature and easy to connect.

So the connection developed like this:

+-+ Physical Line +-+-+ | Terminal | | Modem | | Modem | | UART | | Computer | +-+-- -+ | | +-+

The Terminal on the left is all kinds of teletype.

Modem is used on both sides of the physical circuit, which is what we often call "cat". That is because the network has gradually become developed, and we can share the connection. (probably speculate, it may be wrong)

UART can be understood as a device that converts teletype signals into signals that can be recognized by a computer.

Kernel TTY subsystem

In order to support these teletype, the computer designs a subsystem called TTY, which has the following internal structure:

+-- + | Kernel | | +-+ | | +-+ -+ | | +-+ | | UART | | Line | | TTY | | User process A | +-+ | | driver | | discipline | | driver | | User process B | | +- -+-+ | +- -- +

UART driver docking external UART equipment

Line discipline mainly deals with input and output. It can be understood that it is a part of TTY driver.

TTY driver is used to handle all kinds of terminal devices.

The process of user space deals with the terminal through TTY driver

For simplicity, UART driver and Line discipline are no longer listed separately in the following introduction, but can be considered part of TTY driver

TTY equipment

For each terminal, TTY driver creates a TTY device corresponding to it. If multiple terminals are connected, this is what it looks like:

+-+ | TTY Driver | +-+ | +-+-+ | | | | User process A | | Terminal A | | ttyS0 | | +-+-+ | User process B | | +-+ | +-+ | | +-+ | +-+-+ | User process C | | Terminal B | | ttyS1 | | +-+-+ | | User process D | | +-+ | +-+ | | +-+ |

When the driver receives a connection from a terminal, it creates a corresponding tty device according to the terminal model and parameters (the device name in the above figure is ttyS0 because most terminal connections are serial connections). Because each terminal may be different and has its own special commands and usage habits, the configuration of each tty device may be different. For example, when you press the delete key, some characters may be deleted, while others may be deleted later. If it is not configured correctly, it will cause some buttons not to be the desired behavior. This is why when we use analog terminals, if the default configuration is not in line with our habits, we need to do some personalized configuration.

Later, with the continuous development of computers, teletype devices gradually disappeared, we no longer need special terminal devices, each machine has its own keyboard and display, each machine can be the terminal of other machines, remote operation is realized through ssh, but the architecture of kernel TTY driver has not changed. If we want to interact with the processes in the system, we still need to use TTY devices. As a result, there are a variety of terminal simulation software, and simulate several common terminals, such as VT100, VT220, XTerm and so on.

You can list all terminal types supported by the system by using the command toe-a

You can compare the difference between the two terminals by calling infocmp, for example, infocmp vt100 vt220 will output the difference between vt100 and vt220.

How programs deal with TTY

Before discussing how TTY devices are created and configured, let's take a look at how TTY is used by processes:

# first use the tty command to see which ttydev@debian:~$ tty/dev/pts/1# the current bash is associated with. See which processes have opened the tty. Dev@debian:~$ lsof / dev/pts/1COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEbash 907 dev 0u CHR 136 0t0 1 0t0 4 / dev/pts/1bash 907 dev 1u CHR 136 dev 1 0t0 4 / dev/pts/1bash 907 dev 2u CHR 136 0t0 4 / Dev/pts/1bash 907 dev 255u CHR 136 CHR 1 0t0 4 / dev/pts/1lsof 1118 dev 0u CHR 136 0t0 4 / dev/pts/1lsof 1118 dev 1u CHR 136 0t0 4 / dev/pts/1lsof 1118 dev 2u CHR 136 camera 1 0t0 4 / dev/pts/1# write data directly into tty is the same effect as writing standard output dev@dev:~$ echo aaa > / dev/pts/2aaa

Pts is also a tty device, and their relationship will be described later.

As you can see from the lsof above, the stdin (0u), stdout (1U), and stderr (2U) of the currently running bash and lsof processes are all bound to this TTY.

The following is a structure diagram of the interaction between the tty and the process and the Iamp O device:

Input +-+ bash W +-> | | bash | | pts/1 | +-+ | | +- -+ | +-+-+ | | Terminal Emulator | | tty2 | | User processes | | Monitor | +-+ | Terminal | | Monitor | | +-+ | Terminal |- -+ | Monitor | +-+ | Terminal | | Monitor | | tmux client | | +-+-+ | +-+-+ | | | | ↑ | +-+-+ | +-+ | ptmx | | pts/2 | | shell | +-| -+ + | +-+ | | ↑ | Kernel | ↑ | +-|-- |-+ | wampr | +-+ | | | fork | | ↓ | | +-+ | | | tmux server |

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report