Re: POSIX wait and SIGCHLD

Donn Cave (donn@u.washington.edu)
22 Mar 1995 19:27:08 GMT

scharf@EMBL-Heidelberg.DE writes:
...
| What I want to do is the following:
|
| def wait_some_children():
| while 1:
| pid,status=posix.waitpid(-1,posix.WNOHANG)
| if not pid:
| return

For sure, that's common requirement, for programs that fork asynchronous
proccesses, and it sounds like there's at least no disagreement in principle,
that it would be a good thing to export WNOHANG.

In versions of python where that's not available, I think you'd find that
a signal handler for SIGCHLD will work everywhere. I want to reiterate
that the C examples you're seeing belong to another time, when there really
were "BSD" and "USG" computers. The modern day heirs of these branches
of UNIX all partake of both streams, and more importantly, mostly comply
with the POSIX 1003.1 system interface standards. (Also the reason why
python's attention to POSIX standards pays off.)

As one of the Richard Stevens quotes mentioned, you do have to account for
the fact that system calls may be interrupted by delivery of SIGCHLD when
you have a handler - e.g., if you're blocking in socket.accept(), you may
return with a UNIX error EINTR, which just means that you need to keep
calling socket.accept() until you get some other result. In python that
seems to mean handling an IOError exception:

signal.signal(signal.SIGCHLD, sigchld)

while 1:
try:
... blocking system call
break
except IOError, val:
if val[0] == 4: # 4 == EINTR
continue
else:
raise sys.exc_type, sys.exc_value

There could be better ways to do this.

Donn Cave, University Computing Services, University of Washington
donn@cac.washington.edu