questions about pseudo termial

I am studying APUE(advanced programming in the unix environment). I have read up to chapter19 pseudo terminal. I know that the pty is like a fake device for programs to direct STDIN and STDOUT to. But I don't know why we need it. Can someone tell me what is the use of pseudo terminal. Thanks.

If you know why we need a terminal, it will be easy for you to know why we need a pseudo terminal. A terminal doest'nt just get your raw inputs and displays them, but also process them, eg: color, font, escape sequences. the ways terminals work vary, like vt100 and x-term. A pseudo terminal is a software amulated terminal to meet the need on internet.

back in the day systems only had physical terminals(tty) - eg. old school green screens. Remote sessions use pty - eg. telnet, ssh.

Several reasons, but it comes down to the way terminals are used on a UNIX system.

Things run by cron or system services don't get a terminal, and this is a problem. What if your ssh keys are messed up, and it prints "type password:" into /dev/null and hangs forever? UNIX solved this problem by adding three features:

  • The 'controlling terminal'. The kernel remembers which terminal a program belongs to, if any. Any interactive session uses a terminal.
  • The special '/dev/tty' device, which opens your terminal again if you need it. Programs which really, really need to talk to a human and not whatever program's next in the pipe chain can open /dev/tty and print directly to their screen.
  • The isatty() system call, which lets a program tell whether stdin/stdout/stderr is a terminal or not.

So when you do command | ssh username@host othercommand ssh will see that you're logged in interactively, and open your terminal directly to ask you for a password.

Of course, these days, most terminals aren't real, physical terminals. So a virtual terminal device is used to get all the side-effects of a real terminal without actually being in a terminal.

1 Like

Thanks, man. Your reply gives me some insights.