Teletypes
Overview
A teletype (TTY) is an electromechanical device used to send and receive typed messages, e.g. telegrams. Before being connected to any computer, they were originally connected to one another in a network called Telex. Teletypes were eventually connected to computers once the command line replaced the old batch processing model.
To handle the multiple different teletype models, a software compatibility layer was introduced. In Unix, the kernel handles all low-level details (e.g. word length, baud rate, flow control, parity, control codes for rudimentary line editing, etc.). Color output, sophisticated cursor movements, etc., availabile on later terminals (e.g. the VT-100 series) was left to the application.
A TTY may go by other names as well:
- Terminal. The electronics point of view. A terminal refers to the end of an electric wire.
- Console. The furniture point of view. Usually refers to a physical TTY in the home.
On Linux, TTY device files are found under /dev. The /dev/tty file is a synonym for the controlling terminal of a process, if any. The tty command is used to determine the controlling TTY for a shell. There are a number of ways to configure TTY devices:
- The C library
<termios.h>. - The
ioctlsyscall. - The
sttycommand line tool.
TTY Subsystem
Line Discipline
A line discipline (LDISC) is a software module inside the kernel's TTY subsystem. It processes and transforms streams of characters passing between the TTY driver and applications in userspace.
The default line discipline is N_TTY (named for "normal TTY"). If in canonical mode, the LDISC performs basic buffering and editing of lines. If in raw mode, it typically does minimal to no processing.
TTY Driver
The TTY driver is the low-level interface that talks directly to the actual hardware or functional backend. That is, it transmits data between the kernel and the actual TTY device.
A TTY driver is associated with one foreground process group at a time. Processes in background process groups that attempt to read or write from the TTY are stopped with a SIGTTIN and SIGTTOU signal respectively. When shells suspend and/or resume processes, they must inform the relevant TTY driver of these changes.
Virtual Terminals
A virtual terminal (VT) is a virtualized console provided by the operating system's kernel. On Linux machines, these can be accessed using Ctrl-Alt-F<N>. The VTs accessed in this way correspond to TTYs /dev/tty<N>.
Pseudoterminal
A pseudoterminal (PTY) refers to a pair of connected virtual devices called the master and the slave. The slave device appears just like a standard terminal - all operations that can be applied to a TTY can be applied to a PTY slave.