MyTetra Share
Делитесь знаниями!
What does kill 0 do actually?
Время создания: 04.06.2017 15:39
Раздел: root - Linux
Запись: Yurons/mytetra/master/base/14965871688nibqvtihf/text.html на raw.github.com

Like it says, it sends the signal to all the members of the process group of the caller.

Process groups are used to implement job control in the shell (they can be used for other things, but interactive shell job control is the main reason for their existence).

You'll notice that when you type Ctrl-C, all the processes of the current jobs are killed, not only the one that started them. Also, that doesn't kill the background jobs.

That is achieved with process groups. A job is a group of processes started by a shell which the shell can put in background or foreground (set as the foreground process group of the terminal or not), and kill as a whole.

You can find out about process group ids and session ids with ps -j (j for Job control).

To kill the process group of PGID $x, you do:

kill -- "-$x"

kill 0 kills the process group of the caller.

Note that if you do: /bin/kill 0, the shell will start a new job to execute that kill command, so kill will only kill itself.

kill is usually a shell builtin though, so kill will kill the process group of the shell.

However, when the shell is interactive, it is the process managing process groups, so typically there's no other process in the process group of the shell. All the processes started by the shell, are in other process groups:

$ sleep 1000 &
[1] 22746
$ ps -j
  PID  PGID   SID TTY          TIME CMD
22735 22735 22735 pts/23   00:00:00 zsh
22746 22746 22735 pts/23   00:00:00 sleep
22749 22749 22735 pts/23   00:00:00 ps

Above, sleep and ps are in two different process groups, one in background, one in foreground and they are different from the process group of the shell.

You could do though:

(man kill & sleep 1; ps -j; kill 0)

The interactive shell would start a new process group for that subshell, and both the subshell and man (and the other commands started by man like your pager, groff...) would be in the same process group, so kill 0 would work there. (the sleep above is to give enough time for the pager to start so we can see it in the ps -j output before we kill it).

Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования