what does kill
exactly do?
I have a parent process which is creating 100 (as an example) child processes one after another. At the end of any child's job, I kill the child with kill(pid_of_child, SIGKILL)
and I cannot see that in ps
output. But if something goes wrong with the parent process and I exit from the parent process with exit(1)
(at this point only 1 child is there - I can check tht in ps
), at that point I see a lot of <defunct>
processes whose ppid
is pid
of parent process.
How is that possible? did kill
not kill the child processes entirely?
kill
doesn't kill anything. It sends signals to the target process. SIGKILL
is just a signal. Now, the standard action for SIGKILL
-- the only action, actually, since SIGKILL
can't be handled or ignored by a process -- is to exit, that's true.
The "<defunct>" process is a child that hasn't been reaped, meaning that the parent hasn't called wait()
to retrieve the exit status of the child. Until the parent calls wait()
, the defunct (or "zombie") process will hang around.
Whenever a process ends, no matter how it ends (kill
or otherwise), it will stay in the kernel's process table until its parent process retrieves its exit status (with wait
and friends). Leaving it in the process table avoids a number of nasty race conditions.
If your parent process has exited, the children should get reassigned to init
, which periodically reaps its children.
Yes, SIGKILL terminates the process, but in any case (either normal exit or terminated), processes have an exit status, which needs to be kept around for potential readers - as such an entry in the process table may remain until this is done. See http://en.wikipedia.org/wiki/Zombie_process .
来源:https://stackoverflow.com/questions/8468866/kill-does-it-kill-the-process-right-away