Terminal

Overview

History

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.

Job Control

The protocol for allowing a user to move between multiple process groups (or jobs) within a single login session.

Process Group

Jobs

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.

Foreground Job

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

Background Job

Other process groups managed by the shell that are executing without such access to the terminal are called background jobs.

Orphaned Process Group

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.

Session

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

Controlling Terminal

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.

28.1 Concepts of Job Control (The GNU Library)

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.

API

POSIX termios

Linux ioctl

Pseudo-terminal Interface

The way programs like xterm and emacs implement their terminal emulation functionality.

Slave

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.

Master

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.

The GNU Library API

Python

tty

Pseudo-terminal

Usage

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.

Python documentation - pty

Implementation

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.

Internal

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.

TTY Line Discipline

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.

TTY Driver

Characters Handling

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.

Reference

Last updated