I've seen several questions like this one, but after trying various variants of checking if the children are alive and exiting the children processes, I simplified the problem and still it will not work.
Am I wrong in exiting a forked process with sys.exit(0)? Is there another way to kill it. The thing is, I cannot have the parent kill the processes since it won't know when they're done working.
At first I thought it was because a executed a system command before exiting (Python run system command and then exit... won't exit), but I've even removed that in the simplified version as given solution didn't work either.
Here's an example:
import sys
import os
import time
children = []
for i in range(0,3):
pid = os.fork()
if pid == -1:
continue
elif pid == 0:
# Do work...
print 'Child %d spawned' % os.getpid()
sys.exit(0)
else:
children.append(pid)
time.sleep(5)
for pid in children:
proc_path = '/proc/%d' % pid
if not os.path.exists(proc_path):
print 'Child %d is dead' % pid
else:
print 'Child %d is alive' % pid
This prints:
Child 27636 spawned
Child 27637 spawned
Child 27638 spawned
Child 27636 is alive
Child 27637 is alive
Child 27638 is alive
But child processes should be dead.
What causes these processes to become zombies in this case?