UNIX pseudo terminal


Recently, I am studying the relevant theoretical knowledge about UNIX, and I record relevant important concept memos here.


A pseudo terminal means that to an application, it looks like a terminal, but in fact it is not a real terminal.

Usually a process opens a pseudo-terminal master and calls fork. The child process establishes a new session, opens a corresponding pseudo-terminal slave device, copies its file descriptor to stdin, and then calls exec. The pseudo-terminal slave device is called the controlling terminal of the child process.

looks like a bidirectional pipe, from the device on theterminal line disciplineThis allows us to have additional processing power that ordinary pipes do not have.

Typical Uses of Pseudo Terminals

network login server

The most typical examples are telnetd and rlogind servers.
There are two exec calls between the rlogind server and the login shell, because the login program usually checks whether the user is valid between the two execs.

Window system terminal emulation

A terminal emulator acts as an intermediary between a shell and a window manager. Each shell executes in its own window.
The shell connects its own standard input, standard output, and standard error to the slave side of the PTY.

script program

The script program copies all input and output information during a terminal session into a file.
The downside of using script is having to deal with control characters in the file.

expect program

Drives interactive mode operation in non-interactive mode.

run co-process

When communicating with co-processes through pipes, the standard I/O library fully buffers standard input and standard output, causing deadlocks.
Now the standard input and standard output of the co-process act like terminal devices, so the standard I/O library will set these two streams to be line buffered.

Watch the output of a long-running program

Since redirecting to a file, the standard I/O library will fully buffer its standard output.

Open pseudo terminal device

PTYs behave like physical end devices, so applications don't care what device they're using.
Before a fake device is available, its permissions must be set so that applications can access it.