Linux pseudo-terminal (Pty)

By " Linux terminal (TTY) " article we learned: We often say that the terminal into the terminal tty1-6 and pseudo terminals. Tty1-6 case of using Linux system is generally directly connected the keyboard and display, or the like using vSphere console virtualization scheme, pseudo terminal is used in other cases. This article describes the basic concept of a pseudo terminal. This article demonstrates the use of part of the environment is ubuntu 18.04.

Pseudo-terminal

Pseudo-terminal (pseudo terminal, sometimes referred to as pty) it refers to a pseudo-terminal master and slave pseudo-terminal device of this character. Wherein the slave corresponding to / dev / file in a pts / directory, and is identified as a master file descriptor (fd) in memory. Pseudo terminal emulator is provided by the terminal, the terminal emulator is an application that runs in user mode.

Master end is closer to the user a display, an end of the keyboard, slave end CLI is running on a virtual terminal (Command Line Interface, Command Line Interface) program. Linux pseudo terminal driver, will master terminal (such as a keyboard) data is written to the forward end of the slave input for the program, the program writes the slave terminal to the master terminal for forwarding data (display driver, etc.) is read. Please refer to the following schematic (in this figure from the Internet):

We open the terminal desktop applications, such as GNOME Terminal, it is a kind of terminal emulation software. When the terminal emulation software is running, it creates by opening / dev / ptmx file a pseudo-terminal master and slave pairs and let the slave end of the shell to run. When the user presses a key in the terminal emulation software, which generates a stream of bytes and write master, the process can read the input from the shell of the slave; the shell and its subroutines, output is written in the slave, responsible for printing characters into the window by the terminal emulation software.

Pseudo terminal usage scenarios

Pseudo-terminal about three types of usage scenarios:

  • Terminal emulation software like xterm, gnome-terminal like graphical user interface keyboard and mouse events to convert the input text, and graphically displaying output
  • Relay between the input and output terminals on the pseudo terminal and a server on a remote client remote shell application (e.g. sshd)
  • Multiplexer applications, such as screen and tmux. They relay the input and output from a terminal to another, so that the text-mode application detached from the actual terminal

Why Linux to be presented this pseudo-terminal concept? command-line shell and other procedures can not be read directly from the monitor and keyboard data?
To run multiple terminal emulator with the screen, and remote login, really can not let the shell directly across the pseudo-terminal this layer. In the operating system of a big idea - under the guidance of virtualization, multiple terminal emulators, remote user is assigned multiple virtual terminals are necessary. Slave end shell using the above figure is a virtual terminal. Master side end user interactive simulation. It is called virtual terminal, because it looks like in addition to forwarding the data stream, but also a little terminal.

The principle of pseudo-terminal

Is a pair of character the user equipment terminal emulator running state created essentially the pseudo terminal. Wherein the slave corresponding to / dev / file in a pts / directory, and is identified as a master file descriptor (fd) in memory. For a pseudo terminal, the focus is terminal emulation software to run in user space, which is the essential difference between it and the terminal, please refer to the following diagram:

/ Dev / ptmx is a character device file, when the process opens / dev / ptmx file, the process will also get a point pseudoterminal master (ptm) a file descriptor and pseudoterminal slave created in the / dev / pts directory (pts ) equipment. By opening each file descriptor / dev / ptmx document obtained is an independent ptm, it has its own associated pts, ptmx (memory can be considered a ptmx objects) within the file descriptor and maintains the pts the correspondence relationship of the read and write file descriptors will be forwarded to the corresponding ptmx pts. We can see ptmx open file descriptors by lsof command:

$ sudo lsof /dev/ptmx

The default IO process

Under normal circumstances the way we are connected by remote command execution, the standard input, standard output and standard error output process will be bound to a 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;
}

This code is stored in the file mydemo.c, and then execute the following command to compile and execute the program:

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

demo program output of the PID own processes, another now open a terminal execute lsof command:

$ lsof -p 17981

0u process can be seen (stdin), 1u (stdout), 2u (standard error) are bound to the pseudo-terminal / dev / pts / 0.

 

Reference:
the Linux the TTY / the PTS Overview
The TTY demystified
pseudo terminal pts man page
pseudo terminal pty man page

Guess you like

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