What are pseudo terminals (pty/tty)?
Tried to flag it for migration, but Linux/Unix is not a valid migration target. Trying gradient descent, Super User is already a slightly better target than this site, and maybe they can route it to an even better site.
3 Answers 3
What is a pseudo terminal? (tty/pty)
A device that has the functions of a physical terminal without actually being one. Created by terminal emulators such as xterm. More detail is in the manpage pty(7).
Why do we need them? How they got introduced and what was the need for it?
Traditionally, UNIX has a concept of a controlling terminal for a group of processes, and many I/O functions are built with terminals in mind. Pseudoterminals handle, for example, some control characters like ^C.
Are they outdated? Do we not need them anymore? Is there anything that replaced them?
They are not outdated and are used in many programs, including ssh.
Thanks @thiton. Can you please elaborate on your example of ssh ? How does it use pty/tty? How does master/slave role play in ssh ?
ssh allocates a pty for the shell it creates. The shell is connected to the slave end and thereby can rely on all the capabilities of a normal terminal (e.g. termcap if I remember correctly), and the ssh daemon connects to the master end and sends and receives its input there.
The answer is in the name — «Pseudo» meaning «not genuine but having the appearance of».
With the first terminals, there was always a piece of hardware attached with associated device, be it display hardware or a serial port.
With xwindows, telnet and ssh, there came a need for software «Pseudo devices» to do the job of standing in for display hardware. They are «Pseudo Terminals» . software that emulates Terminal hardware, handling input and output in the same way a physical device would so that the software connected is not aware there’s not a real device attached.
What is pty in linux
NAME
pty - pseudoterminal interfaces
DESCRIPTION
A pseudoterminal (sometimes abbreviated "pty") is a pair of virtual character devices that provide a bidirectional communication channel. One end of the channel is called the master; the other end is called the slave. The slave end of the pseudoterminal provides an interface that behaves exactly like a classical terminal. A process that expects to be connected to a terminal, can open the slave end of a pseudoterminal and then be driven by a program that has opened the master end. Anything that is written on the master end is provided to the process on the slave end as though it was input typed on a terminal. For example, writing the interrupt character (usually control-C) to the master device would cause an interrupt signal (SIGINT) to be generated for the foreground process group that is connected to the slave. Conversely, anything that is written to the slave end of the pseudoterminal can be read by the process that is connected to the master end. Pseudoterminals are used by applications such as network login services (ssh(1), rlogin(1), telnet(1)), terminal emulators, script(1), screen(1), and expect(1). Data flow between master and slave is handled asynchronously, much like data flow with a physical terminal. Data written to the slave will be available at the master promptly, but may not be available immediately. Similarly, there may be a small processing delay between a write to the master, and the effect being visible at the slave. Historically, two pseudoterminal APIs have evolved: BSD and System V. SUSv1 standardized a pseudoterminal API based on the System V API, and this API should be employed in all new programs that use pseudoterminals. Linux provides both BSD-style and (standardized) System V-style pseudoterminals. System V-style terminals are commonly called UNIX 98 pseudoterminals on Linux systems. Since kernel 2.6.4, BSD-style pseudoterminals are considered deprecated (they can be disabled when configuring the kernel); UNIX 98 pseudoterminals should be used in new applications. UNIX 98 pseudoterminals An unused UNIX 98 pseudoterminal master is opened by calling posix_openpt(3). (This function opens the master clone device, /dev/ptmx; see pts(4).) After performing any program-specific initializations, changing the ownership and permissions of the slave device using grantpt(3), and unlocking the slave using unlockpt(3)), the corresponding slave device can be opened by passing the name returned by ptsname(3) in a call to open(2). The Linux kernel imposes a limit on the number of available UNIX 98 pseudoterminals. In kernels up to and including 2.6.3, this limit is configured at kernel compilation time (CONFIG_UNIX98_PTYS), and the permitted number of pseudoterminals can be up to 2048, with a default setting of 256. Since kernel 2.6.4, the limit is dynamically adjustable via /proc/sys/kernel/pty/max, and a corresponding file, /proc/sys/kernel/pty/nr, indicates how many pseudoterminals are currently in use. For further details on these two files, see proc(5). BSD pseudoterminals BSD-style pseudoterminals are provided as precreated pairs, with names of the form /dev/ptyXY (master) and /dev/ttyXY (slave), where X is a letter from the 16-character set [p-za-e], and Y is a letter from the 16-character set [0-9a-f]. (The precise range of letters in these two sets varies across UNIX implementations.) For example, /dev/ptyp1 and /dev/ttyp1 constitute a BSD pseudoterminal pair. A process finds an unused pseudoterminal pair by trying to open(2) each pseudoterminal master until an open succeeds. The corresponding pseudoterminal slave (substitute "tty" for "pty" in the name of the master) can then be opened.
FILES
/dev/ptmx (UNIX 98 master clone device) /dev/pts/* (UNIX 98 slave devices) /dev/pty[p-za-e][0-9a-f] (BSD master devices) /dev/tty[p-za-e][0-9a-f] (BSD slave devices)
NOTES
A description of the TIOCPKT ioctl(2), which controls packet mode operation, can be found in tty_ioctl(4). The BSD ioctl(2) operations TIOCSTOP, TIOCSTART, TIOCUCNTL, and TIOCREMOTE have not been implemented under Linux.
SEE ALSO
select(2), setsid(2), forkpty(3), openpty(3), termios(3), pts(4), tty(4), tty_ioctl(4)