Why use a pts for xterm or anything

terminalxterm

I keep reading on multiple sites that pts device files are used for pseudo-terminals which are "not directly connected to the hardware" (ssh, telnet).

Since xterm running on you machine is directly connected the output on your keyboard and the display on your screen, why does this use a pseudo-terminal device file?

I read the other question regarding ttys pts, … and it didn't seem to cover this..

Also, what is the need for a "pseudo terminal," if it's ultimately a device file with input and output why does it matter what it's coming from?

Thanks in advance

Best Answer

There are several things about terminals that are managed inside the kernel, such as keyboard signals (mapping e.g. Ctrl+Z to SIGSTOP), managing foreground and background process groups, an extremely crude line edition mechanism, sending SIGHUP when the terminal goes away, and other niceties.

With a hardware terminal, the terminal device sits between the application and the hardware. The kind of terminal device depends on the kind of hardware, for example (on Linux) /dev/tty1 for a PC keyboard and screen, /dev/ttyS0 for a terminal connected over a serial line, /dev/ttyUSB0 for a terminal connected over USB, etc.

+-------------+         +-----------------+         +-------------+
| text mode   |         | kernel          |         | peripheral  |
| application |<------->| e.g. /dev/ttyS0 |<------->| e.g. on a   |
|             |         |                 |         | serial port |
+-------------+         +-----------------+         +-------------+

With a terminal emulator, the principle is the same, with the connection still going through a kernel driver, but what's at the other end is also a process.

+-------------+         +-----------------+         +-------------------+
| text mode   |         | kernel          |         | terminal emulator |
| application |<------->| /dev/pts/*      |<------->| e.g. xterm, sshd, |
|             |         |                 |         |      screen, ...  |
+-------------+         +-----------------+         +-------------------+

Xterm, telnetd¹, sshd and the like are not (directly) connected to any hardware. When you type a key on a keyboard, the information goes through the keyboard (the physical device) to the keyboard driver inside the kernel which then relays it to a process that's listening on the keyboard device. If you're in a graphical session, that process is an X server which then relays the information to the process whose window is focussed. Conversely, when a program running inside xterm produces output, xterm transforms this into an order for the X server to display certain characters in a certain font at a certain position. There is no hardware involved anywhere near the process that's processing the input or producing the output, and possibly none at all (e.g. if the xterm window is not visible on the screen, or at least none locally if the terminal is provided by a telnet or ssh server).

¹ The terminal is on the remote (server) side.