1

The Linux Programming Interface says SIGHUP is sent to the controlling process of a terminal

When a terminal window is closed on a workstation. This occurs because the last open file descriptor for the master side of the pseudoterminal associated with the terminal window is closed.

My understanding is that a terminal window is created for a slave side, and a master side can have multiple slave sides. So when a terminal window is closed, it only means the last open file descriptor for the slave side of the pseudoterminal associated with the terminal window is closed. Why does the quote say the "master" side?

Thanks.

  • The terminal emulator program has the master side of the pseudoterminal; when the terminal emulator program exits it can only close the master side. The slave side is open in the child process which runs the shell or whatever program runs in the terminal emulator. How could it be otherwise? Think of a real physical terminal: it has an outside and an inside. For a pseudoterminal, the "ouside" part is called the master and the "inside" part the slave. – AlexP Jan 2 at 4:59
  • It can't have multiple slave side. Think it as a pipe, just a tube with two holes. And the window you see on screen doesn't have any direct relationship with pseudo-terminal devices. It's just the terminal-emulator program is rendering data from the pseudo-terminal devices to an X window (or Wayland window, or DRM device, whatever). – 炸鱼薯条德里克 Jan 2 at 9:37
  • Back to your question, closing that window (usually, if you're nit-picking, remember your question about closing window and terminating process?) causes(this logic happens in userspace) the terminal emulator process exit, which is holding the master pty fd, not the slave fd(at least non of the terminal emulator program I know). – 炸鱼薯条德里克 Jan 2 at 9:46
3

My understanding is that a terminal window is created for a slave side, and a master side can have multiple slave sides.

A pseudo terminal has always just one master side and one slave side. It's just a bidirectional pipe with some extra ops [1].

A terminal emulator which can open more than one window/tab will also handle more than one pseudo tty master.

As I already explained in another answer, the terminal emulator can do its own thing when the user tries to close the window or one of its tabs; For instance, xterm will not close the master side of the pty, but will just send a SIGHUP to the process group of the tty, and only destroy the window (and exit) when the process started in in has exited or it's not able to use itself the master part of the pty anymore (eg. because all handles to its slave side were closed).

[1]. On a SystemV system with STREAMS, those extra ops are modular and have to be "pushed" with ioctl(I_PUSH). On Linux and *BSD, they're hardwired. Also, the behavior of ptys is not completely standardized; trying to read from a slave with no master or vice-versa will fail with EIO on Linux but return 0 (EOF) on FreeBSD.

  • xterm doesn't? Why? Wouldn't this cause any problems because processes don't get EIO any more? By the way, xterm support tabs? Is xterm just getting new version or Is there something wrong with my brain? – 炸鱼薯条德里克 Jan 2 at 9:52
  • Wow, I never knew that about xterm. Open an xterm and run kill -STOP $$. Then the close button on xterm does not work. You have to kill the xterm e.g. using xkill :-). gnome-terminal lets you close the window though. @炸鱼薯条德里克 I don't think anyone said anything about xterm and tabs. – sourcejedi Jan 2 at 10:13
  • @炸鱼薯条德里克 I don't think there's any standard that says what a program should do on having its window(s) closed, but pulling the rug from under the stuff running in it is not something that I will appreciate. (eg. I've hacked qemu to send a power down instead of knocking down the vm on having the sdl window closed). – mosvy Jan 2 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.