Re: How to wait *all* children processes to return?



loudking <loudking@xxxxxxxxx> writes:
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.

[...]


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;

}

This program will create a lot more than five processes.
.