How to wait for *all* children to return ?



Question: write a program which creates 5 processes (in addition to
itself). One of these processes must display 1, another must display
2
.... the last one displays 5. The parent process waits until all other
processes are finished, then returned.

My solution is that

/* Header files omitted */

#define NUMBER_PROCESS 5

void sig_chld(int sig)
{
pid_t pid;
int stat;

while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
{
;
}
signal(SIGCHLD, sig_chld);
}

int main(int argc, char *argv[])
{
pid_t pid = getpid();
int i;

signal(SIGCHLD, sig_chld);

for (i = 0; i < NUMBER_PROCESS && pid > 0; i++)
{
pid = fork();
if (pid < 0)
{
perror("Error fork");
exit(-1);
}
else if (pid == 0)
{
printf("%d\n", i+1);
}
}/* for i */

return 0;
}

Should I add a for loop outside the while loop in sig_chld function
to
make sure that exactly 5 child termination are captured?

.



Relevant Pages

  • Re: PID controllers
    ... few examples from the industrial temperature controller market: ... single line display vs dual line display ... Now using the cheaper PID and charging the same for a kit/install as ... some do for the more expensive PID kit/install is another matter ...
    (alt.coffee)
  • Re: PID controllers
    ... There rarely is any free lunch. ... Of course a $50 PID is not going to be the 100% equivalent of a $150 unit, ... few examples from the industrial temperature controller market: ... single line display vs dual line display ...
    (alt.coffee)
  • creator 3d in sun ultra10: unable to launch Xsun
    ... caiman-root% fbconfig -list ... The X-server can not be started on display:0 ... error (pid 337): Server for display:0 can't be started. ...
    (comp.unix.solaris)
  • Re: returning page before long process is finished using C
    ... if (pid == 1){ ... you want to do the processing (close your filedescriptors ... i want to display right away _exit; ... // print some error message that couldn't fork _exit;} else {// do the long processing here? ...
    (comp.infosystems.www.authoring.cgi)
  • How to wait *all* children processes to return?
    ... One of these processes must display 1, ... int main ... pid_t pid = getpid; ... Should I add a for loop outside the while loop in sig_chld function ...
    (comp.unix.programmer)