Pseudo Terminals (PTY)
Pseudo Terminals (PTYs) provide software-based terminal devices that emulate physical terminals. They allow applications such as terminal emulators, remote login services, and terminal multiplexers to communicate with interactive programs through a terminal interface.
Learning Objectives
- Understand pseudo terminals.
- Learn PTY architecture.
- Study master and slave devices.
- Learn the PTY allocation process.
- Understand
posix_openpt(). - Prepare for PTY programming.
1. What is a Pseudo Terminal?
A pseudo terminal is a pair of virtual character devices that provide terminal communication entirely through software. One end is controlled by an application, while the other end behaves like a normal terminal device for another process.
2. Why are PTYs Used?
Pseudo terminals enable applications to interact with terminal-oriented programs without requiring a physical terminal device. They are widely used by remote login software, terminal emulators, and terminal multiplexing applications.
3. PTY Architecture
The master device is controlled by an application, while the slave device behaves like a standard terminal for the connected process.
4. Master and Slave Devices
| Device | Purpose |
|---|---|
| Master PTY | Controlled by the application. |
| Slave PTY | Acts as the terminal for another process. |
5. posix_openpt()
The POSIX posix_openpt() function opens a new
pseudo-terminal master device and returns its file
descriptor.
| Prototype |
|---|
int posix_openpt(int flags);
|
6. grantpt()
The POSIX grantpt() function changes the
ownership and permissions of the slave pseudo-terminal
associated with a master device.
This prepares the slave device for use.
| Prototype |
|---|
int grantpt(int fd);
|
7. PTY Allocation Workflow
A PTY is allocated by opening the master device, preparing the slave device, obtaining its pathname, and opening the slave terminal.
8. Practical POSIX Example
The program opens a pseudo-terminal master device and prepares its associated slave device using POSIX functions.
9. Interview Questions
- What is a pseudo terminal?
- Why are PTYs used?
- What is the difference between a physical terminal and a PTY?
- What are the master and slave devices?
- What does
posix_openpt()do? - Why is
grantpt()required? - Which device is controlled by the application?
- Which device behaves like the user's terminal?
- What is the first step in allocating a PTY?
- Which POSIX function opens the master PTY?
10. unlockpt()
The POSIX unlockpt() function unlocks the slave
pseudo-terminal associated with a master PTY.
The slave device cannot normally be opened until it has been
successfully unlocked.
| Prototype |
|---|
int unlockpt(int fd);
|
11. ptsname()
The POSIX ptsname() function returns the pathname
of the slave pseudo-terminal associated with the specified
master PTY.
The returned pathname can then be used to open the slave
device.
| Prototype |
|---|
char *ptsname(int fd);
|
12. Opening the Slave PTY
After obtaining the slave pathname using
ptsname(), the slave device is opened using the
standard open() function.
Once opened, the slave behaves like a normal terminal device.
13. PTY Session Creation
These steps establish a terminal session between the master and slave devices.
14. PTY Data Flow
Data written to one side of the PTY becomes available for reading on the opposite side, providing bidirectional communication.
15. Complete PTY Allocation Sequence
| Step | Function |
|---|---|
| 1 | posix_openpt() |
| 2 | grantpt() |
| 3 | unlockpt() |
| 4 | ptsname() |
| 5 | open() |
16. Practical POSIX Example
The example performs the standard PTY allocation sequence by opening the master device, preparing the slave device, unlocking it, and retrieving the slave pathname.
17. Linux Commands
| Command | Description |
|---|---|
tty |
Display the current terminal device. |
who |
Display logged-in users and terminals. |
w |
Display active users and terminal sessions. |
ls -l /dev/pts |
Display active pseudo terminals. |
ps -t pts/0 |
Display processes attached to a PTY. |
18. Interview Questions
- Why is
unlockpt()required? - What does
ptsname()return? - Why must the slave PTY be opened separately?
- Which function retrieves the slave PTY pathname?
- What is the complete PTY allocation sequence?
- Which side behaves like a normal terminal?
- How is data exchanged between the master and slave PTYs?
- Can the slave PTY be opened before calling
unlockpt()? - Which system call opens the slave PTY?
- Which Linux directory contains active PTY devices?
19. openpty()
The openpty() function creates a new pseudo
terminal by allocating both the master and slave devices in a
single call.
Unlike the POSIX PTY allocation sequence,
openpty() is a BSD extension and is not part of
the POSIX standard.
| Prototype |
|---|
int openpty(int *master,
int *slave,
char *name,
const struct termios *termp,
const struct winsize *winp);
|
20. forkpty()
The forkpty() function combines pseudo-terminal
allocation, process creation, and terminal setup into a
single function call.
It is commonly used by terminal emulators and remote login
applications.
Like openpty(), it is a BSD extension rather
than a POSIX interface.
| Prototype |
|---|
pid_t forkpty(int *master,
char *name,
const struct termios *termp,
const struct winsize *winp);
|
21. PTY-based Process Creation
A child process created using a pseudo terminal communicates through the slave PTY, while the parent process interacts with the master PTY. This arrangement allows the child process to behave as though it is connected to a normal terminal.
22. PTY Lifecycle
A pseudo terminal exists for the duration of the associated communication session and is released when both ends are closed.
23. POSIX vs BSD/Linux Interfaces
| Function | Category |
|---|---|
posix_openpt() |
POSIX |
grantpt() |
POSIX |
unlockpt() |
POSIX |
ptsname() |
POSIX |
openpty() |
BSD Extension |
forkpty() |
BSD Extension |
login_tty() |
BSD Extension |
24. PTY Communication Model
The parent controls the terminal session through the master device, while the child process interacts with the slave device as its controlling terminal.
25. Practical Example
The example allocates a master and slave pseudo terminal
using the BSD openpty() interface.
26. Linux Commands
| Command | Description |
|---|---|
tty |
Display the active terminal. |
ps -ef | grep pts |
Show processes attached to PTYs. |
who |
Display logged-in terminal sessions. |
w |
Display active users and terminals. |
ls /dev/pts |
List active pseudo terminals. |
27. Interview Questions
- What is the purpose of
openpty()? - Is
openpty()defined by POSIX? - What additional functionality does
forkpty()provide? - Which process communicates through the master PTY?
- Which process uses the slave PTY?
- Why are BSD PTY interfaces convenient?
- When should
posix_openpt()be preferred overopenpty()? - What happens after both PTY endpoints are closed?
- What is the role of
login_tty()? - Why are PTYs widely used by terminal emulators?
28. Best Practices
- Prefer POSIX PTY interfaces for portable applications.
- Always check the return value of PTY allocation functions.
- Close both master and slave file descriptors when they are no longer required.
- Release PTY resources before program termination.
- Use BSD PTY functions only when the target platform supports them.
- Handle terminal attributes separately using the
termiosinterface. - Document platform-specific PTY behavior.
29. Common Programming Errors
| Error | Description |
|---|---|
| Ignoring return values | PTY allocation failures remain undetected. |
Skipping grantpt() |
Slave permissions may not be prepared correctly. |
Skipping unlockpt() |
The slave PTY cannot be opened. |
| Leaving descriptors open | System resources remain allocated unnecessarily. |
| Using BSD-only functions without portability checks | Applications may not build or run on all POSIX systems. |
30. Real-World PTY Applications
| Application | How PTYs Are Used |
|---|---|
| Terminal Emulators | Provide a virtual terminal for interactive shells. |
| Remote Login Services | Connect remote users to login shells through PTYs. |
| Terminal Multiplexers | Create and manage multiple virtual terminal sessions. |
| Automation Tools | Interact programmatically with terminal-based applications. |
| Interactive Debuggers | Communicate with applications that require a terminal. |
31. PTY Function Summary
| Function | Purpose | Category |
|---|---|---|
posix_openpt() |
Open a PTY master device. | POSIX |
grantpt() |
Prepare slave permissions. | POSIX |
unlockpt() |
Unlock the slave device. | POSIX |
ptsname() |
Retrieve the slave pathname. | POSIX |
openpty() |
Allocate master and slave PTYs. | BSD Extension |
forkpty() |
Create a child process with a PTY. | BSD Extension |
login_tty() |
Assign a controlling terminal. | BSD Extension |
32. APUE / POSIX vs BSD/Linux
| Feature | POSIX | BSD/Linux Extension |
|---|---|---|
| PTY Allocation | posix_openpt() |
openpty() |
| Slave Preparation | grantpt(), unlockpt() |
Handled internally by openpty() |
| Retrieve Slave Name | ptsname() |
Returned by openpty() |
| Create Child Session | Separate fork() and setup |
forkpty() |
| Portability | High | Platform-dependent |
33. Module Summary
This module introduced pseudo terminals, their architecture, master and slave devices, PTY allocation, session creation, and communication flow. It also covered the standardized POSIX PTY interfaces and the BSD convenience functions that simplify PTY programming on supported systems.
34. Key Takeaways
- A pseudo terminal consists of a master device and a slave device.
- The master device is controlled by an application.
- The slave device behaves like a standard terminal.
- The POSIX PTY allocation sequence is
posix_openpt(),grantpt(),unlockpt(),ptsname(), andopen(). openpty()andforkpty()are BSD extensions.- PTYs enable interactive communication without requiring physical terminal hardware.
- Terminal emulators and remote login applications rely heavily on PTYs.
- Using POSIX interfaces improves application portability.
Concept Mastery Assessment — Module 66
Verify your conceptual understanding with 12 targeted technical assessment questions. Instant feedback and explanations are provided for every choice.