Pseudo terminal
A pseudo-terminal is a service in a Unix environment that is usually provided by the kernel .
The pseudo-terminal is a type of interprocess communication , a bidirectional pipe that interprets the data flow and reacts specifically to configurable events. These additional functions of the pseudo-terminal are the same as those associated with a normal (e.g., serial) terminal within the kernel .
Pseudo-terminals are used when a terminal-oriented application (well-known examples are vim , top or less ) is not to be operated on a normal terminal but, for example, by means of an application such as telnet or ssh over a network. If you were to use the standard output of these processes directly, this would make little sense, since a terminal always interprets the inputs, i.e. H. performs certain actions for certain special characters.
functionality
A normal terminal enables text-oriented input / output. One side is always linked to a process , that is, the process has opened a device file of the terminal ( /dev/ttyS0
, /dev/tty1
...). The other side of the terminal is z. B. connected to a serial interface ( ) or to a virtual system console (monitor and keyboard) ( ) (n = 0, 1, 2…). With the pseudo-terminal, the other side is not connected to a device, i.e. H. an interface of the computer, but connected to another process. A pseudo-terminal has a master and a slave side. The slave side is the equivalent of a normal terminal device used by an application; the master side (in the kernel) is directly connected to the slave side. The master is what the serial interface is in a normal terminal, except that this is done with another process instead of with a device. That is why a pseudo-terminal is also a form of interprocess communication. In contrast to a pipe or a Unix domain socket , the data is interpreted during the transit from master to slave (or vice versa). If certain escape sequences are to be transmitted, they are not passed directly to the process connected to the slave, but trigger a certain action. For example, a SIGINT signal is sent to the process that opened the slave side when 0x03 is written to the master ( + ). If + (0x1A) is written to the master, then the process that opened the slave (more precisely: that made the slave his controlling tty ) receives a SIGTSTP signal, which usually stops it.
/dev/ttySn
/dev/ttyn
StrgCStrgZ
The device driver for a normal terminal also has these functions; the interprocess connection is supplemented by these functions for a pseudo terminal.
When it says: a process writes to the master, then it is comparable to: a terminal device transmits data to the computer via the serial interface (for example because the user has entered something on the keyboard). The additional function of a pseudo-terminal compared to a pipe is therefore marginal, but a shell will not be able to provide important functions (job control, flow control, character-oriented input) with a pipe. Pseudo terminals are often used for network-transparent terminal connections ( telnet , ssh ) and for GUI- based terminal emulations ( xterm ).
Programs that use pseudo terminals
- screen
- a window manager for text mode
- xterm
- a terminal emulation for X11
- sshd
- Secure terminal server
Implementations
There are two basic implementations of pseudo terminals: the BSD implementation and the UNIX 98 implementation (SUSv2) .
After opening, both implementations behave exactly the same.
New Unix operating systems (current Linux versions, FreeBSD ...) mostly provide both implementations.
BSD
With the BSD implementation there are already “prepared” pseudo terminals in / dev . The master terminals are named according to the scheme / dev / ptyp n (n = 1,2,3…), the slave terminals are called / dev / ttyp n .
UNIX 98 (SUSv2)
There is a master file /dev/ptmx
(pseudo terminal multiplexer) that you can open as often as you like. When opened, a file descriptor for a master terminal is returned. A new slave terminal is allocated each time. The slaves are then called z. B. /dev/pts/1
, /dev/pts/23
...