mukul gautam in Knowledge Lovers (Every Bee's Hive), Students, System Admin in Linux intern • Armour infosec May 30, 2020 · 1 min read · 1.4K

Terminal types in Linux subsystem

The terminal subsystem consists of three layers
  • The upper layer implements the character device interface ( open, read, write, close …)

  • The line discipline

  • The lower layer which communicates with the hardware or the pseudoterminal.

    Terminal types in Linux subsystem

A Unix based system processes communicate with the terminal through terminal character device file, all the text we write in the terminal are store in the input buffer of this file and then the process may read from this to get the data. 

In this mechanism file descriptor 0 SDTIN used for read data from device file and file descriptor 1 STDOUT used for writing data to the file this type of terminal are called hardware-based terminal used in the past.

Now we emulate the terminal using software, a personal computer can run terminal emulator software that replicates the function of a terminal, sometimes allowing concurrent use of local programs and access to a distant terminal host system. The terminal emulators on most Unix-like systems such as gnome-terminal, qterminal, xterm, do emulate physical terminal including support for escaping sequences.

X windows system the program that controls the display, drawing the boxes and buttons.  Suppose we open a terminal emulator in X windows and start writing text in the terminal, the text we write goes from the X window server to terminal emulator which writes the text in the input buffer of "master" pseudo-terminal character device file. The operating system copies data from master to its associated "slave" pseudo-terminal character device file to be read by the process. Each terminal emulator has its own master and slave pseudo-terminal pair.

Usually, the master is connected to a terminal emulator (such as xterm) and the slave is connected to a program being run, most commonly a shell (such as bash). Thus, the slave behaves exactly like a classical terminal

dev/tty : 

TTY is the abbreviation of Tele-Typewriter (or Tele-Type)Originally, a TTY was physically connected to a Unix machine as an input/output device, usually on an RS-232 cable. The input was fed on a keyboard. In the beginning, the computer's output was going to a printer and later also to screens It is a special kind of file, it doesn't represent the character device. When a program opens /dev/tty file they get the file descriptor for the so-called controlling terminal that is terminal associated with their process. In simple words, it used for controlling the terminal file. A TTY (or terminal) device is a special class of a character device.

dev/pts :

When a process opens /dev/ptmx, it gets a file descriptor for a pseudoterminal master (PTM), and a pseudoterminal slave (PTS) device is created in the /dev/pts directory.  Each file descriptor obtained by opening /dev/ptmx is an independent PTM with its own associated PTS, whose path can be found bypassing the file descriptor to its name. Before opening the pseudoterminal slave, you must pass the master's file descriptor to grantpt() and unlockpt(). Once both the pseudoterminal master and slave are open, the slave provides processes with an interface that is identical to that of a real terminal. Data written to the slave is presented on the master file descriptor as input. Data written to the master is presented to the slave as input.

for detail explanation follow: