Re: posix.waitpid(pid,WNOHANG) ??

Guido.van.Rossum@cwi.nl
Tue, 21 Mar 1995 16:46:56 +0100

> Unfortunately, WNOHANG is not exported from posix nor defined
> anywhere else in pyhton. Even more unfortunately, WNOHANG has a
> different value on different UNIX flavors. E.g:

> sunos 4.1.3 : #define WNOHANG 1
> Solaris 2.4 : #define WNOHANG 0100
> OSF1 2.0 : #define WNOHANG 0x1
> Ultrix 4.4 : #define WNOHANG 1
> irix 4.0.5C : #define WNOHANG 0x1
> irix 5.2 : #define WNOHANG 0100
> irix 6.0.1 : #define WNOHANG 0100

I know. There's a little-known and undocumented file WAIT.py in some
machine dependent parts of the library that defines WNOHANG (and a
bunch of other symbols) for certain architectures,
e.g. Lib/sunos4/WAIT.py and Lib/irix5/WAIT.py, but it appears to be
missing for other architectures and it seems to differ from what you
list for SunOS 4 :-(. It's easy enough to create WAIT.py for various
architectures (see the "regen" shell script in the irix5 directory)
but it's not done automatically since it seems to require manual
intervention on every new platform :-( :-( :-( Contributions are
welcome however.

> Q: Is there another way to wait without hanging?
>
> I tryied to install a signal handler on SIGCHLD. But for some reasons, I
> don't wait for all children, because some signals get lost, or even worse
> I get into a state, where I don't get any SIGCHLD signal anymore. After a
> while my process can't fork anymore because there are too many unwaited
> children (on irix 6.0.1) :-(.

There's undocumented special code in signalmodule.c which requires
your handler to re-instate the signal handler for this signal, because
otherwise the two-stage way of handling signals in Python would cause
infinite recursion given the way this signal is generated (if there's
still a child waiting when you return from the C signal handler, the
signal is sent again by the OS). I don't know if this code works
correctly -- in fact I don't know what the guaranteed and actual
semantics of SIGCHLD are in Posix and other Unix versions.

> Q: Would it make sense to export WNOHANG from the posix module?

Yes. It's not in the current beta of 1.2 and I would prefer to add as
few things as possible. However if you come up with a clean patch for
posixmodule.c that adds these wait-related symbols, I might be
convinced to add them. (Something similar has already been done with
socket related symbols.)

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>