12

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

trap : SIGTERM SIGINT SIGKILL # SIGKILL is pointless.

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?

3 Answers 3

11

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)

2
1

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.

5
  • 3
    -1: Killing a parent does not cause the children to be killed.
    – camh
    Nov 6, 2012 at 6:46
  • 2
    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. Nov 6, 2012 at 7:40
  • 2
    @CharlesBoyd That comment should be part of your answer.
    – jordanm
    Nov 6, 2012 at 8:01
  • 4
    @CharlesBoyd: Add that to your answer and I'll give you +1 instead of -1. Your answer currently reads as though child processes are killed when a parent process is killed (BTW, its session-based, not process group-based).
    – camh
    Nov 6, 2012 at 9:05
  • @CharlesBoyd I understand that, but the point of the question is how does it "manipulate[] a job by sending signals" when it receives a SIGKILL. I see no way for it to use the killpg system call at that point.
    – Kevin Cox
    Nov 6, 2012 at 13:20
0

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.

2
  • I never said it did, I said that it dies but survives when it is in a script and that is killed.
    – Kevin Cox
    Nov 6, 2012 at 13:17
  • Doesn't survive in a script, for me.
    – tumbleweed
    Nov 6, 2012 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .