5

I'm trying to see what happens when I try to kill the kernel_task process. Since kill -9 pid is the command to force quit a process, and kernel_task's pid is 0, I simply ran kill -9 0.

Instead of getting Permission Denied or a Operation not permitted when killing a root process, you simply get this:

Last login: Thu Jun 18 16:55:10 on ttys005
MacBook-Air:~ james$ kill -9 0

[Process completed]

... which is weird because trying to kill launchd (one level below kernel_task) gives you this:

-bash: kill: (1) - Operation not permitted

and all other root processes.

Why the heck does it just terminate the process that called the command, instead of sending Operation not permitted, like all the other root processes?

Bonus points if you can tell me the answer to this question: If you create a script that tries to kill kernel_task (and ignores SIGTERM), running it will output Killed: 9 and return to bash. Why does it put that into stdout when it kills it? Why not something more descriptive?

1 Answer 1

4

and kernel_task's pid is 0

They probably use the PID 0, because it is a reserved PID and as such you cannot send signals to the process.

I simply ran kill -9 0.

The *nix command kill is more or less a direct front-end for the kill system call. From the documentation of the system call:

If pid is zero:
      Sig is sent to all processes whose group ID is equal to the process group ID of the sender, and
      for which the process has permission; this is a variant of killpg(2).

In other words, in your case, kill -9 0 is just a fancy way to send the (fatal) signal to itself. (More about process groups.)

Why the heck does it just terminate the process that called the command

The kill (unless you have tweaked the shell settings) is a built-in shell command. In other words, the shell calls the kill syscall and that causes it to kill itself.

Try the same but with the external kill command, e.g. /bin/kill -9 0. In that case only the /bin/kill would be killed.

4
  • What does IOW mean? Commented Jun 19, 2015 at 8:16
  • IOW - in other words
    – Dummy00001
    Commented Jun 19, 2015 at 8:34
  • So in other words, 0 is a reserved pid just for kernel_task, but it also means "yourself", just like the IP address 127.0.0.1 means yourself. Commented Jun 21, 2015 at 14:59
  • Something like this. PID 0 in case of the kill pointing to process group is part of the POSIX interface of the kill. Under Mac OS X, kernel_task having PID 0 is a detail of realization of the POSIX. That seemingly conflicts with the POSIX, but in practice it doesn't, since POSIX doesn't allow PID 0 being used by a real process.
    – Dummy00001
    Commented Jun 21, 2015 at 15:29

You must log in to answer this question.

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