Terminal I/O
Terminal I/O provides a standardized interface for interacting
with terminal devices in UNIX-like operating systems.
The POSIX termios interface allows applications
to configure terminal behavior, including input processing,
output processing, control characters, and communication
settings.
Learning Objectives
- Understand terminal devices.
- Learn controlling terminals.
- Study the
termiosinterface. - Understand terminal attributes.
- Learn
tcgetattr(). - Learn
tcsetattr().
1. What is Terminal I/O?
Terminal I/O is the mechanism through which applications
communicate with terminal devices for user input and output.
The POSIX termios interface provides portable
functions for configuring terminal behavior.
2. Terminal Devices
A terminal device is a character-oriented device that enables interaction between users and applications. Examples include physical terminals, serial terminals, and terminal emulator windows.
3. Controlling Terminal
A controlling terminal is the terminal associated with a session. It provides input to foreground processes and receives their output. Signals generated from keyboard input are typically delivered through the controlling terminal.
4. The termios Interface
The POSIX termios interface defines terminal
attributes that control input processing, output processing,
control modes, local modes, and special control characters.
5. Terminal Attributes
| Category | Description |
|---|---|
| Input Modes | Configure input processing. |
| Output Modes | Configure output processing. |
| Control Modes | Configure hardware communication. |
| Local Modes | Configure local terminal behavior. |
| Control Characters | Configure special character handling. |
6. tcgetattr()
The POSIX tcgetattr() function retrieves the
current terminal attributes associated with a terminal
device.
| Prototype |
|---|
int tcgetattr(int fd,
struct termios *termios_p);
|
7. tcsetattr()
The POSIX tcsetattr() function modifies the
terminal attributes associated with a terminal device.
Applications typically retrieve the existing settings,
modify selected fields, and then apply the updated
configuration.
| Prototype |
|---|
int tcsetattr(int fd,
int optional_actions,
const struct termios *termios_p);
|
8. Practical POSIX Example
The program retrieves the current terminal attributes and
applies them again without modification using the POSIX
tcgetattr() and tcsetattr()
functions.
9. Interview Questions
- What is Terminal I/O?
- What is a terminal device?
- What is a controlling terminal?
- What is the purpose of the
termiosinterface? - What does
tcgetattr()do? - What does
tcsetattr()do? - Why should an application retrieve existing attributes before modifying them?
- Which structure stores terminal attributes?
- Which file descriptor usually represents the terminal input?
- Why is Terminal I/O standardized by POSIX?
10. Canonical Mode
Canonical mode, also known as line-oriented mode, processes input one complete line at a time. The terminal driver performs line editing functions before the application receives the input.
| Feature | Description |
|---|---|
| Input Delivery | Complete line after Enter is pressed. |
| Line Editing | Handled by the terminal driver. |
| Typical Use | Interactive command-line programs. |
11. Non-Canonical Mode
Non-canonical mode allows an application to receive input without waiting for an entire line. Input processing is controlled through terminal attributes, making this mode suitable for interactive applications.
| Feature | Description |
|---|---|
| Input Delivery | Characters are available immediately or according to configured rules. |
| Line Editing | Performed by the application if required. |
| Typical Use | Text editors, games, terminal utilities. |
12. Input Processing Flags
Input mode flags determine how incoming characters are processed before they are delivered to an application.
| Flag | Purpose |
|---|---|
IGNBRK |
Ignore break condition. |
ICRNL |
Translate carriage return to newline. |
INLCR |
Translate newline to carriage return. |
ISTRIP |
Strip the eighth bit from input characters. |
IXON |
Enable software flow control on input. |
13. Output Processing Flags
Output mode flags determine how characters are processed before being transmitted to the terminal device.
| Flag | Description |
|---|---|
OPOST |
Enable output processing. |
ONLCR |
Translate newline to carriage return/newline. |
14. Control Modes
Control mode flags configure communication characteristics such as character size, parity generation, and receiver behavior.
| Flag | Description |
|---|---|
CS8 |
Use 8-bit characters. |
PARENB |
Enable parity generation. |
CREAD |
Enable the receiver. |
CLOCAL |
Ignore modem control lines. |
15. Local Modes
Local mode flags determine terminal behavior that is handled locally by the terminal driver.
| Flag | Description |
|---|---|
ECHO |
Echo typed characters. |
ICANON |
Enable canonical mode. |
ISIG |
Generate terminal signals. |
IEXTEN |
Enable implementation-defined extensions. |
16. Canonical vs Non-Canonical
| Feature | Canonical | Non-Canonical |
|---|---|---|
| Input Unit | Entire line | Characters or configured blocks |
| Line Editing | Terminal Driver | Application |
| Enter Key Required | Yes | No |
| Typical Applications | Shells | Editors, games |
17. Practical POSIX Example
The example disables canonical mode by clearing the
ICANON local mode flag before applying the
updated terminal attributes.
18. Linux Commands
| Command | Description |
|---|---|
stty -a |
Display terminal settings. |
tty |
Display the current terminal device. |
reset |
Restore terminal settings. |
clear |
Clear the terminal screen. |
infocmp |
Display terminal capability information. |
19. Interview Questions
- What is canonical mode?
- What is non-canonical mode?
- What does the
ICANONflag control? - What is the purpose of the
ECHOflag? - What is the difference between input and output processing flags?
- What does
OPOSTcontrol? - Why is
IXONused? - What is the role of control mode flags?
- Why do text editors typically use non-canonical mode?
- Which command displays the current terminal configuration?
20. Special Control Characters
The termios structure contains an array of
control characters that define how special keyboard input is
processed by the terminal driver.
| Control Character | Purpose |
|---|---|
VINTR |
Generate interrupt signal. |
VQUIT |
Generate quit signal. |
VERASE |
Erase previous character. |
VKILL |
Erase the current input line. |
VEOF |
Indicate end-of-file during input. |
VSTART |
Resume software-controlled output. |
VSTOP |
Suspend software-controlled output. |
21. VMIN
VMIN specifies the minimum number of characters
that must be available before a read operation can complete
when the terminal operates in non-canonical mode.
| Setting | Behavior |
|---|---|
| 0 | Read may return immediately. |
| Greater than 0 | Wait until the required number of characters is available. |
22. VTIME
VTIME specifies a timeout value used by read
operations in non-canonical mode.
The timeout is measured in tenths of a second.
| Setting | Behavior |
|---|---|
| 0 | No timeout. |
| Greater than 0 | Timeout after the configured interval. |
23. Baud Rates
Baud rate specifies the communication speed used by serial
terminal devices.
The termios interface provides standardized
functions for retrieving and modifying terminal input and
output speeds.
| Constant | Speed (bits/second) |
|---|---|
B9600 |
9600 |
B19200 |
19200 |
B38400 |
38400 |
B115200 |
115200 |
24. cfgetispeed()
The POSIX cfgetispeed() function retrieves the
configured terminal input baud rate.
| Prototype |
|---|
speed_t cfgetispeed(const struct termios *termios_p);
|
25. cfsetispeed()
The POSIX cfsetispeed() function modifies the
configured terminal input baud rate.
| Prototype |
|---|
int cfsetispeed(struct termios *termios_p,
speed_t speed);
|
26. Output Baud Rate Functions
The POSIX functions cfgetospeed() and
cfsetospeed() retrieve and configure the terminal
output baud rate.
| Function | Purpose |
|---|---|
cfgetospeed() |
Retrieve output speed. |
cfsetospeed() |
Configure output speed. |
27. VMIN and VTIME Relationship
In non-canonical mode, VMIN and
VTIME together determine when a read operation
returns to the application.
28. Practical POSIX Example
The program configures the terminal input and output baud rates using POSIX speed-setting functions before applying the updated terminal configuration.
29. Linux Commands
| Command | Description |
|---|---|
stty speed |
Display the terminal baud rate. |
stty 9600 |
Set the terminal baud rate. |
stty -icanon |
Disable canonical mode. |
stty echo |
Enable character echo. |
stty sane |
Restore commonly used terminal settings. |
30. Interview Questions
- What is the purpose of
VINTR? - What does
VEOFrepresent? - What is the purpose of
VMIN? - How is
VTIMEmeasured? - Why are
VMINandVTIMEused only in non-canonical mode? - What is a baud rate?
- What does
cfgetispeed()return? - What is the difference between
cfsetispeed()andcfsetospeed()? - Why should baud rates match between communicating devices?
- Which POSIX functions configure terminal communication speed?
31. Terminal State Management
Applications that modify terminal attributes should preserve the original terminal configuration before making changes. When the application completes, the original settings should be restored to maintain normal terminal behavior.
32. Saving and Restoring Terminal Attributes
Saving the original terminal attributes allows applications to restore the terminal to its previous state before exiting.
33. Best Practices
- Always save the original terminal settings.
- Restore terminal attributes before program termination.
- Check the return value of every terminal function.
- Modify only the required terminal flags.
- Use POSIX interfaces for portability.
- Document non-default terminal behavior.
- Avoid unnecessary changes to communication parameters.
34. Common Errors
| Error | Description |
|---|---|
| Not restoring terminal settings | Leaves the user's terminal in an unexpected state. |
| Ignoring function return values | Configuration failures may go unnoticed. |
| Overwriting all attributes | Unintentionally changes unrelated terminal settings. |
| Incorrect baud rate configuration | Serial communication may fail. |
| Using non-canonical mode unnecessarily | Increases application complexity. |
35. APUE / POSIX vs Linux
| Feature | Category |
|---|---|
termios |
POSIX |
tcgetattr() |
POSIX |
tcsetattr() |
POSIX |
cfgetispeed() |
POSIX |
cfsetispeed() |
POSIX |
cfgetospeed() |
POSIX |
cfsetospeed() |
POSIX |
stty |
Linux/UNIX Utility |
reset |
Linux/UNIX Utility |
36. Module Summary
This module introduced the POSIX terminal interface, including terminal attributes, canonical and non-canonical input processing, control characters, communication speeds, and terminal configuration functions. Proper management of terminal state ensures reliable and portable terminal-based applications.
37. Key Takeaways
termiosdefines standardized terminal configuration.tcgetattr()retrieves terminal attributes.tcsetattr()applies updated terminal settings.- Canonical mode processes complete input lines.
- Non-canonical mode allows immediate or timed input processing.
VMINandVTIMEcontrol non-canonical read behavior.- Baud rates define terminal communication speed.
- Applications should always restore the original terminal configuration before exiting.
- POSIX terminal interfaces provide portability across UNIX-like systems.
- Proper terminal management prevents unexpected user terminal behavior.
Concept Mastery Assessment — Module 65
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.