os.fork() question?

Ishwar Rattan rattan at cps.cmich.edu
Tue Sep 14 22:19:11 CEST 2004


Erik Heneryd <erik at heneryd.com> wrote in message news:<mailman.3271.1095110255.5135.python-list at python.org>...
> Ishwar Rattan wrote:
> > Info at http://doc.python.org/ on os.fork() says that
> >   it has 'unix' semantics (on a UNIX box) on return values
> >   child pid in parent, 0 in child, no mention of failure?
> > 
> > So, what does it return on failure? I tried it under Linux
> > with Python-2.3.4, after few thousand forks the system just hangs!
> > (does not return/report fork failure)
> 
> It raises OSError.
> 
> 
> Erik
The following code when executed kills controlling xterm+X-window system,
but the system does not hang.

-ishwar
---
import os, signal, sys, time

def child():
    signal.pause()
    print 'this from child with pid: ', os.getpid()
    sys.exit(2)

def main():
    print 'forking child..'
    while 1:
       try:
           cpid = os.fork()
       except OSError:
           raise 'fork failed..'
       if cpid == 0:
           child()
       else:
           print 'fork success..', cpid
       
main()
-----



More information about the Python-list mailing list