163

This might be really basic question but I want to understand it thoroughly.

  1. What is a pseudo terminal? (tty/pty)
  2. Why do we need them? How they got introduced and what was the need for it?
  3. Are they outdated? Do we not need them anymore? Is there anything that replaced them?
  4. Any useful use-case?

What I did:

  1. Read man pages - got some info but not the exact picture.
  2. Tried to read on them from Unix Network Programming by Richard Stevens. Got some info but not the why? part.
hari
  • 2,470
  • I think so, too, especially if no specialized issues like openpty/forkpty arise here. –  Sep 20 '11 at 18:31
  • 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. –  Sep 20 '11 at 19:25
  • 17
    This is a pretty good overview: http://www.linusakesson.net/programming/tty/index.php – nos Sep 20 '11 at 20:00
  • @nos : wow! Thats an awesome link. The thing that I was looking for. Thanks a ton :) – hari Sep 20 '11 at 20:06
  • 1
    Chapter 62 and Chapter 64 of The Linux Programming Interface may be helpful – zjk Feb 15 '16 at 11:00
  • This is an another explanation from quora, https://www.quora.com/What-is-the-difference-between-a-terminal-emulator-and-a-terminal-Arent-all-terminals-terminal-emulators-Is-it-the-difference-between-a-TTY-and-a-pseudo-TTY – kundy Apr 24 '18 at 03:06

3 Answers3

85

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.

Any useful use-case?

ssh.

Stephen Kitt
  • 434,908
thiton
  • 2,310
  • 3
    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? – hari Sep 20 '11 at 18:17
  • 6
    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. –  Sep 20 '11 at 18:24
40

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.

gview
  • 573
  • 1
    Because now we have many applications which need to access same piece of hardware, every app is using the hardware via "pseudo" terminal? – hari Sep 20 '11 at 18:15
  • I expanded the answer a bit -- hope it clarifiies. –  Sep 20 '11 at 18:29
  • No, we just don't actually have that kind of hardware around anymore. xterm emulates a classic terminal, that's all. –  Sep 20 '11 at 18:29
  • @thiton: When you say emulates, thats the software part, isn't it? – hari Sep 20 '11 at 18:48
  • @hari: Yes, it is. A pseudo terminal has no hardware components. –  Sep 20 '11 at 19:17
  • 1
    So using the word "terminal" in UNIX context today is actually wrong? Because everything is a "pseudo-terminal" ? – A. Sallai Jun 08 '17 at 07:29
  • Yes, so long as someone doesn't actually have an old vt100 or xterminal lying around they decide to hook up. – gview May 23 '18 at 19:25
15

Pseudo-terminals are emulators for serial lines. They provide endpoints for telnet, ssh, and xterm shells.

Joshua
  • 1,893
  • 2
    And they're ancient relics (really, shells/text applications are still interacting over a device that emulates a tele-type writer over a serial line like they worked 40 years ago...) We still need them because there is no replacement :-( – nos Sep 20 '11 at 17:55
  • Well, if you consider the terminal, I don't think you need much more than a stream of letters comming and going... – Diego Sevilla Sep 20 '11 at 17:59
  • 5
    @Diego Sevilla The original unix inventor did away with tty/ptys in Plan 9 , and there a terminal pretty much does just use a stream of in/out data. But in *nix, the ttys are still around and used by consoles and terminal emulators to e.g. control terminal size, flow control, line buffering, special key events, and other stuff. – nos Sep 20 '11 at 18:06
  • 7
    @nos: It turns out they're not the ancient relics we're lead to believe. Trying to do without them in Windows is rather painful in the end. The use of powershell remoting is hampered by one thing: interactive console programs don't work and can't be fixed to work right. There is no possibility of a reasonable text editor like either DOS EDIT or vi. – Joshua Dec 15 '15 at 17:43
  • 2
    @Joshua Another evidence to the contrary is the original designers of Unix created the Plan 9 OS, where they completely did away with ttys, yet they achieved remoting in that manner (and much more) just fine. – nos Dec 15 '15 at 19:23