views:

151

answers:

3

Hi,

In my Python script, I first launch a subprocess by subprocess.Popen(). Then later on, I want to kill that subprocess by kill -9 Pid.

What I found is that after the kill is executed, the subprocess is "stopped" because the GUI window of that process disappeared immediately. But when I perform a "ps aux" right after the kill, the same process (with same pid) is still shown in the result. The difference is the command of the process is included in a pair of () like below:

root 30506 0.0 0.0 0 0 s000 Z+ 6:13PM 0:00.00 (sample process)

This breaks my process detect logical since the dead process still can be found by ps.

Anyone know why this is happening?

Thanks!

A: 

I think -9 signal lets the process to try to handle kill and spend some time housekeeping. You can try just kill the process without signal.

Edit: oh, its actually -15 signal, that lets process die gracefully. never mind.

RocketSurgeon
The KILL signal can be handled, but the program will be terminated after that. If the process was doing housekeeping it would not be in 'zombie' state.
Gonzalo
How can SIGKILL be handled? In Python, `signal.signal(signal.SIGKILL, handler)` raises a `RuntimeError: (22, 'Invalid argument')` exception on systems that support `sigaction()`. In C you can install a signal handler via `signal(2)` for SIGKILL, but it is not called.
mhawke
+3  A: 

From the manual page of ps:

Z Defunct ("zombie") process, terminated but not reaped by its parent.

That means that the parent didn't do a waitpid() for the child that died.

Apart from waitpid(), you can avoid that by using a double fork when executing the child.

Gonzalo
A: 

Zombie processes are actually just an entry in the process table. They do not run, they don't consume memory; the entry just stays because the parent hasn't checked their exit code. You can either do a double fork as Gonzalo suggests, or you can filter out all ps lines with a Z in the S column.

ΤΖΩΤΖΙΟΥ