waste of resources ?

Carey Evans c.evans at clear.net.nz
Wed Jun 9 13:26:47 CEST 1999

Arne Mueller <a.mueller at icrf.icnet.uk> writes:


> > def sigchild_handler(signum, frame):
> >     os.wait()


> Hm, that doesn't work properly  with my program. THe number of zombie
> processes is steadily at about 20, which means there are 20 unhandled
> died children.  

Unix doesn't guarantee you'll get exactly one signal delivered per
event.  To be sure to get all the children, you need to loop over the
processes with waitpid().

I think something like this should do it:

def sigchld_handler(signum, frame):
    print "Checking for processes..."
        while 1:
            pid, sts = os.waitpid(-1, os.WNOHANG)
            if pid == 0:
            print "Process %d ended with status %04x." % (pid, sts)
    except OSError, detail:
        if detail.args[0] != errno.ECHILD:
            print "No more children."
        print "Some children yet to finish."

I can't actually see any way to keep count well enough and avoid race
conditions, to get rid of the try/except.  Sometimes I wish Python had 
POSIX signals, but not badly enough to actually write it.

	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

	     "I'm not a god.  I've just been misquoted."

More information about the Python-list mailing list