Terminal
Last updated
Last updated
Historically, users accessed a UNIX system using a terminal connected via a serial line (an RS-232 connection).
On early UNIX systems, the terminal lines connected to the system were represented by character devices with names of the form /dev/ttyn.
The protocol for allowing a user to move between multiple process groups (or jobs) within a single login session.
The processes belonging to a single command are called a process group or job, e.g., command like:
Processes can be put in another process group using the setpgid
function, provided the process group belongs to the same session.
The shell can give unlimited access to the controlling terminal to only one process group at a time. This is called the foreground job on that controlling terminal
Other process groups managed by the shell that are executing without such access to the terminal are called background jobs.
Process groups that continue running even after the session leader has terminated are marked as orphaned process groups.
When a process group becomes an orphan, its processes are sent a SIGHUP
signal.
Usually, new sessions are created by
the system login program
process disconnecting from its controlling terminal when it calls setsid
to become the leader of a new session
A shell that supports job control must arrange to control which job can use the terminal at any time. Otherwise there might be multiple jobs trying to read from the terminal at once, and confusion about which process should receive the input typed by the user.
One of the attributes of a process. Child processes created with fork
inherit this attribute from its parent.
A session leader that has control of a terminal is called the controlling process of that terminal.
The way programs like xterm
and emacs
implement their terminal emulation functionality.
The slave end of the pseudoterminal provides an interface that behaves exactly like a classical terminal for processes expecting to be connected to a terminal.
Anything that is written on the master end by applications such as
network login services (ssh(1)
, rlogin(1)
, telnet(1)
)
data read from the pseudoterminal master is sent across the network to a client program that is connected to a terminal or terminal emulator
terminal emulators, data read from the pseudoterminal master is interpreted by the emulators in the same way a real terminal would interpret the data
script(1)
,
screen(1)
, and
expect(1)
is provided to the process on the slave end as though it was input typed on a terminal.
Represented by the device file /dev/ptmx
is a character file with major number 5 and minor number 2.
We can use the function spawn
of the Python standard library pty
to open a new pseudo-terminal, spawn a new process, and connect its control terminal to the slave end.
From the source code, we can see that the pty.spawn
function will call a function named fork
which will call os.forkpty
internally to create a new process and a new pair of pseudo-terminal.
The new child process will call os.setsid()
to become the process group leader.
From the implementation of os.forkpty
found in cpython/Modules/posixmodule.c
, we can see that it calls forkpty(3)
to setup the pseudo-terminal.
After getting the original terminal attributes with tcgetattr
and putting stdin in raw mode, as the code:
the function pty.spawn
will then pass data between master and slave ends in a loop, the parent read input from its stdin and then write it back into the master end:
On the other hand, the parent receives the child's output from the master end and then writes it back to its stdout.
We can use strace
to trace pseudo-terminal-related system calls invoked by Python and see that how the input data are read from and write to the master end.
UART driver, which manages the physical transmission of bytes, line discipline instance and TTY driver (drivers/char/tty_io.c
) are referred to as a TTY device.
Every character received by the kernel (both from devices and users) is passed through a preselected TTY Line Discipline.
TTY line discipline process all incoming and outgoing character from/to a tty device in two modes:
canonical mode providing an editing buffer and editing commands likes backspace, erase word, clear line, and reprint
raw mode
The default discipline providing line editing is called N_TTY which is implemented in drivers/char/n_tty.c
.
2 queues exist for handling input characters:
from the terminal device to the reading processes
output characters transmitted from processes to the terminal
If terminal echoing is enabled, then the terminal driver automatically appends a copy of any input character to the end of the output queue, so that input characters are also output on the terminal.