How Can Terminal Emulators Kill Their Children After Recieving a SIGKILL?

From what I understand, SIGKILL cannot be caught. This would imply that a process does not have time to kill it’s children before the OS destroys it. This can be demonstrated with a shell script.

#! /bin/bash


mplayer video.avi

Killing it with SIGKILL leaves mplayer running.

$ kill -9 $pid

But when using a terminal emulator (xterm, Terminal, …) it’s children get killed along with it. How is this possible?

$ mplayer

And kill it:

$ kill -9 $terminal_pid

And mplayer goes down with the ship. Are terminal emulators somehow catching SIGKILL or is there another force at work here?

Asked By: Kevin Cox


Your question answers itself, this happens because these processes are running as children under the terminal emulator. So you kill the terminal emulator, and in doing so, kill all children processes (since the children are running under the same process group as the controlling terminal emulator).

See, for example, the following:

csb@darwin[~]$ ps fauwx | grep -A6 "xfce4-terminal" | awk '{ for (i = 2; i <= 9; i++) $i="" ; print $0 }' 
csb         0:32 xfce4-terminal --geometry=271x65 --display :0.0 --role=Terminal-0x1340050-2606-1351620352 --show-menubar --show-borders --hide-toolbars --working-directory /home/csb --tab --working-directory /home/csb
csb         0:00 _ gnome-pty-helper
csb         0:00 _ bash
csb         0:00 | 
                  _ ssh [redacted]
csb         0:00 _ bash
csb         0:00 _ ps fauwx
csb         0:00 _ grep --color=auto -A6 xfce4-terminal
csb         0:00 _ awk { for (i = 2; i <= 9; i++) $i="" ; print $0 }

All of these processes are running under the ‘xfce4-terminal’ processes, so if I kill that process then it will kill automatically all child processes in the process group…the same way, for example, that exiting the terminal emulator window would necessarily kill my SSH connection.

Programs such as shells create new process groups, usually placing related child processes into a group. Each job is a process group. Outside the kernel, a shell manipulates a job by sending signals to the job’s process group with the killpg system call, which delivers a signal to all the processes in a process group.

Answered By: Charles Boyd

Firstly, I can’t reproduce the mplayer surviving the terminal being killed, using xterm.

The reason it dies, is that it gets a SIGHUP from it’s parent’s death.

Answered By: tumbleweed

The process started by xterm will be the session leader in control of the terminal.

When the terminal goes away, that process automatically receive a SIGHUP signal (followed by a SIGCONT). This is sent by the kernel in a similar way that processes receive SIGINT when you press CTRL-C.

Additionally, a shell may send SIGHUP to some of its children upon exiting (see disown in some shells to disable that)

Answered By: Stéphane Chazelas
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.