Subject: fork, SIGCHLD, wait, waitpid, Debian Linux vs. NetBSD ...
To: None <netbsd-users@netbsd.org>
From: Jeremy C. Reed <reed@reedmedia.net>
List: netbsd-users
Date: 04/11/2001 02:17:34
I have a daemon that forks ten other processes. Under Debian Linux when a
child is terminated, another process is forked (so there is always 11
processes total). Under NetBSD when each child is terminated another is
not started unless it is the last child left (so there are always at least
2 processes). (I want ten children![1])

Here is an example of the code[2]:

void
signal_handler (int signal)
{
  if (signal == SIGCHLD) {
    int stat;
    while (waitpid (-1, &stat, WNOHANG) > 0);
  }
  return;
}

The main part:

  signal(SIGCHLD, signal_handler);
  children = 0; maxchildren = 10;

  while (1) {
    if (children < maxchildren) {
      if (!fork()) {
        mainloop;
        exit(OK);
      } else {
        children++;
      }
    } else {
      wait(NULL);
      children--;
    }
  }

I am guessing that under NetBSD, the signal_handler does the waitpid and
so the second wait() just hangs forever(?).

Any ideas?

Can anyone point me to any *simple* code that does a similar task? (Starts
multiple daemons and forks new ones when they exit.)

Thanks,

   Jeremy C. Reed
   http://www.reedmedia.net/

1) My wife wants six. If I was talking about real kids, I think six is
enough; I already have three.

2) I know some people complain about not having complete working code or
perfect K&R or ANSI code for examples. But I feel that code snippets are
fine. This code is from vm-pop3d and also gnu-pop3d. It is available via
http://www.reedmedia.net/software/virtualmail-pop3d/