what is linux ptys

藏色散人
藏色散人Original
2023-04-20 10:32:532649browse

linux ptys refers to the Linux pseudo-terminal, and the pseudo-terminal refers to the pair of character devices, the pseudo-terminal master and the pseudo-terminal slave; the slave corresponds to a file in the "/dev/pts/" directory. The master is identified as a file descriptor in the memory; the pseudo terminal is provided by the terminal emulator, which is an application running in user mode.

what is linux ptys

#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

What is linux ptys?

Linux pseudo terminal (pty)

The terminals we often talk about are divided into terminals tty1-6 and pseudo terminals. When tty1-6 is used, the Linux system is usually directly connected to the keyboard and monitor, or virtualization solutions such as vSphere console are used. In other cases, pseudo terminals are used. This article will introduce the basic concepts of pseudo terminals. The environment used in the demonstration part of this article is ubuntu 18.04.

Pseudo terminal

Pseudo terminal (sometimes also called pty) refers to the pseudo terminal master and pseudo terminal slave. for character devices. The slave corresponds to a file in the /dev/pts/ directory, and the master is identified as a file descriptor (fd) in memory. Pseudo terminals are provided by terminal emulators, which are applications running in user mode.

The master end is the end closer to the user's monitor and keyboard, and the slave end is the CLI (Command Line Interface, command line interface) program running on the virtual terminal. The Linux pseudo-terminal driver will forward the data written by the master side (such as keyboard) to the slave side for program input, and forward the data written by the program on the slave side to the master side for reading (display driver, etc.). Please refer to the diagram below (this picture comes from the Internet):

The terminal desktop program we open, such as GNOME Terminal, In fact, it is a terminal emulation software. When the terminal emulation software runs, it creates a pseudo-terminal master and slave pair by opening the /dev/ptmx file and letting the shell run on the slave side. When the user presses a keyboard key in the terminal simulation software, it generates a byte stream and writes it to the master. The shell process can read the input from the slave; the shell and its subroutines write the output content to the slave. The terminal emulation software is responsible for printing the characters into the window.

Usage scenarios of pseudo-terminals

There are roughly three types of usage scenarios for pseudo-terminals:

  • Terminal simulation software with graphical interfaces such as xterm, gnome-terminal, etc. convert keyboard and mouse events into text input and graphically display the output content
  • Remote shell applications (such as sshd) relays input and output between a remote terminal on the client and a pseudo-terminal on the server
  • Muxer applications such as screen and tmux. They relay input and output from one terminal to another, detaching text-mode applications from the actual terminal

Why is the concept of pseudo-terminal proposed in Linux? Woolen cloth? Can't command line programs such as shell read data directly from the monitor and keyboard?
In order to run multiple terminal emulators on the same screen and achieve remote login, we really cannot let the shell directly cross the pseudo-terminal layer. Under the guidance of virtualization, a major idea of ​​the operating system, it is necessary to allocate multiple virtual terminals to multiple terminal emulators and remote users. The slave side used by the shell in the picture above is a virtualized terminal. The Master side simulates the interaction on the user side. The reason why it is called a virtualized terminal is that in addition to forwarding data flows, it also looks like a terminal.

Principle of Pseudo Terminal

Pseudo terminal is essentially a pair of character devices created by a terminal emulator running in user mode. The slave corresponds to a file in the /dev/pts/ directory, and the master is identified as a file descriptor (fd) in memory. For pseudo-terminals, the key point is that the software emulation terminal program runs in user space. This is the essential difference between it and the terminal. Please refer to the diagram below:

what is linux ptys

/dev/ptmx is a character device file. When the process opens the /dev/ptmx file, the process will also get a pointer to pseudoterminal master( ptm) file descriptor and a pseudoterminal slave(pts) device created in the /dev/pts directory. Each file descriptor obtained by opening the /dev/ptmx file is an independent ptm, which has its own associated pts. ptmx (it can be considered that there is a ptmx object in the memory) will maintain the file descriptor and pts internally. Corresponding relationship, reads and writes to this file descriptor will be forwarded by ptmx to the corresponding pts. We can view the file descriptors opened by ptmx through the lsof command:

$ sudo lsof /dev/ptmx

Process default IO

Generally when we execute a command through a remote connection, the standard input, standard output and standard error output of the process will be bound to the pseudo terminal. The following is a simple demo program:

#include <stdio.h>#include <unistd.h>int main()
{
    printf("PID : %d\n", getpid());
    sleep(200);

    printf("\n");    return 0;
}</unistd.h></stdio.h>

Save this code in the file mydemo.c, and then execute the following command to compile and execute the program:

$ gcc -Wall mydemo.c -o demo
$ ./demo

The demo program outputs the PID of its own process. Now open another terminal and execute the lsof command:

$ lsof -p 17981

You can see the process's 0u (standard input), 1u (standard output), 2u ( standard error output) are bound to the pseudo terminal /dev/pts/0.

Recommended study: "linux video tutorial"

The above is the detailed content of what is linux ptys. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn