Problem with fork

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Oct 21 08:41:32 EDT 2002


----- Original Message -----
From: "Arief" <stefanus.arief at ai.astra.co.id>


> Hi all,
>
> There is something I don't understand. I've create simple program below in
> my Linux:
>
> #### START OF PROGRAM ####
> #!/usr/bin/python
>
> #
> # MAIN PROGRAM
> #
>
> import os, sys, time
>
> if os.fork(): sys.exit()
> os.setsid()
>
> for child in range(10):
>         time.sleep(1)
>
>         print 'Parent fork'
>         if not os.fork():
>                 # Child process
>                 print 'Child start:', child
>                 time.sleep(child)
>                 print 'Child exit:', child
>                 sys.exit()
> print 'Parent exit'
> #### END OF PROGRAM ####
>
> Parent creates 10 childs in sequence by fork. It works ...
> But, everytime the child process exits, it leaves <defunct>
> in my process status.

The parent process must use os.wait() or an equivalent to
retrieve the exit status of each child; until that happens,
the terminated child appears as a "zombie" process in the
process table.

It's a Unix thing.

Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net






More information about the Python-list mailing list