os.fork() question?

Andrew MacIntyre andymac at bullseye.apana.org.au
Thu Sep 16 15:01:10 CEST 2004


On Wed, 14 Sep 2004, Ishwar Rattan wrote:

> 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.
> >
> 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()

You don't give much detail about your Linux environment, but be aware:
- Python by default is built with threading;
- signals in the presence of threads are a potential issue,
  even though the above code doesn't activate threads, because your
  Python process is using the threaded C library;
- ditto fork();
- various thread implementations have had bugs at various times in
  relation to signals and process management;
- the above code may be being treated as a "fork bomb" by Linux; in which
  case, the Python process(es) is/are summarily killed, possibly leaving
  lots of zombie processes around....

You may care to try building a Python interpreter without threads support
to test whether the some of the issues I mention above are implicated.

-------------------------------------------------------------------------
Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac at bullseye.apana.org.au  (pref) | Snail: PO Box 370
        andymac at pcug.org.au             (alt) |        Belconnen ACT 2616
Web:    http://www.andymac.org/               |        Australia



More information about the Python-list mailing list