OSProcess -OS-X and a unix issue...

Richard A. O'Keefe ok at cs.otago.ac.nz
Wed Mar 6 23:20:14 UTC 2002


John M McIntosh <johnmci at smalltalkconsulting.com> wrote:
	So how do you tell if the process id is valid...

I've never seen kill() used before a waitpid() like that.

Given that
 - you have forked() a child, and you KNOW its pid (cid, say)
 - you have received a SIGCHLD signal, so you know some process has died.
 - you suspect that the dead process is cid, and you want to reap its
   status if it was
there is no need to call kill().  Just
  do r = waitpid(cid, &status, WNOHANG);
     while (r == -1 && errno == EINTR);
  if (r == cid) {
    /* child cid has died, status is its status */
  } else
  if (r == 0) {
    /* child cid hasn't died yet */
  } else
  if (r == -1) {
    /* errno == EINTR:  call was interrupted (handled above) */
    /* errno == EINVAL: bad argument (shouldn't occur) */
    /* errno == ECHILD: there is no such child */
  } else {
    /* should be impossible */
  }

If you don't want to ask about a particular child, but about any child,

  do r = waitpid((pid_t)(-1), &status, WNOHANG);
     while (r == -1 && errno == EINTER);
  if (r > 0) {
    /* r is the pid of a dead child and status is its status */
  } else
  if (r == 0) {
    /* no children have died since the last check */
  } else
  if (r == -1) {
    /* errno == EINTR:  call was interrupted (handled above) */
    /* errno == EINVAL: bad argument (shouldn't occur) */
    /* errno == ECHILD: should not occur in this case */
  } else {
    /* should be impossible */
  }
  
I don't see the point in checking the validity of cid when waitpid()
is going to check it anyway.

Finally, there's really _nasty_ feature of UNIX signals, due to the
address space limitations of PDP-11s.  UNIX signals aren't counted.
(More precisely, they might or might not be.)  If a signal (such as
SIGCHLD) is pending, and another signal of the same kind of pending,
the two are (or may be) merged.  So it's _possible_ to have ten
children die, but only get one SIGCHLD.



More information about the Squeak-dev mailing list