Overview
This lecture explains the fundamental concepts of computer terminals, shells, terminal emulators, PTYs, and related standards, focusing on their evolution and how modern systems interact with them.
What is a Terminal?
- A computer terminal is a device for entering and displaying data to a computer system.
- Early terminals connected via serial lines and often output data to paper.
- Modern UNIX systems manage terminals using the TTY (Teletype) subsystem, which handles input/output streams.
Shells and Terminal Communication
- Terminals and TTYs only move bytes; they require a shell program (like bash or zsh) to interpret input and generate output.
- Shells interact with terminals indirectly through the kernel's TTY interface.
ANSI, ECMA-48, and Terminal Standards
- Early terminals used ASCII plus proprietary codes for features like bold text.
- ANSI created the X3.64 standard, later published freely as ECMA-48, to standardize terminal control codes (escape sequences).
Terminfo and Termcap Databases
- Termcap and terminfo databases map terminal functions (like bold or color) to their required byte sequences.
- Applications use these databases, referenced by the
TERM
environment variable, to produce correct output for different terminals.
Running Programs in a Terminal
- Shells start new programs with the TTY's input/output streams, letting those programs directly interact with the terminal.
- Programs must respect the
TERM
setting and use proper libraries for terminal output.
stdin, stdout, and stderr in Terminals
- UNIX maps
stdin
to TTY input, and both stdout
and stderr
to TTY output.
- From the terminal's perspective, there's only a single output stream, not separate
stdout
and stderr
.
Foreground Processes and Job Control
- Only one process is usually "foreground" and interacts with the TTY, but careful coordination is required to avoid output conflicts.
- Shells can manage which process is foreground for signal delivery via job control.
Signals and Keyboard Shortcuts
- The TTY layer translates keystrokes like CTRL-C (0x03) into signals (
SIGINT
) sent to the foreground process.
- Shells handle these signals for interrupting or suspending processes (e.g., CTRL-Z for suspension).
Terminal Emulators and PTYs
- PTYs (Pseudo Terminal TeletYpes) allow software terminals not tied to physical devices.
- Terminal emulators create PTYs and spawn shell programs, interpreting data to display and encode user input.
Windows and ConPTY
- Traditional Windows terminals worked differently and lacked PTYs, limiting interoperability.
- Newer Windows versions include ConPTY, a pseudo-terminal interface with additional complexity to support legacy console APIs.
- Terminal emulators on Windows may use helpers like
openconsole.exe
or conhost.exe
for PTY support.
Key Terms & Definitions
- Terminal — Hardware or software for input/output to a computer, historically via serial lines.
- TTY (Teletype) — Kernel subsystem managing terminal input/output streams in UNIX.
- Shell — A program like bash that interprets user commands and manages processes.
- ANSI Escape Sequences — Standardized byte codes for controlling terminal display features.
- Termcap/Terminfo — Databases mapping terminal features to their required codes.
- PTY (Pseudo Terminal) — Virtual device providing terminal-like interfaces to software.
- Terminal Emulator — Software that mimics the behavior of hardware terminals within graphical environments.
- ConPTY — The Windows implementation of pseudo-terminals for modern terminal emulation.
Action Items / Next Steps
- Review the terms and standards covered (TTY, PTY, ANSI, terminfo) and how they relate.
- Explore terminal emulators and practice using shell commands with respect to terminal capabilities.
- Optional: Read about ConPTY for deeper understanding of Windows terminal emulation.