#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
void sig_chld(int signo);
void err_sys(const char *p_error);
int
main(int argc, char **argv)
{
int i, number;
pid_t pid;
struct sigaction act;
/* set SIGCHLD signal handler */
act.sa_handler = sig_chld;
if (sigemptyset(&act.sa_mask) == -1)
err_sys("sigemptyset error");
act.sa_flags = 0;
if (sigaction(SIGCHLD, &act, NULL) == -1)
err_sys("sigaction error");
for (i = 1; i <= 10; i++) {
if ((pid = fork()) == -1)
err_sys("fork error");
else if (pid == 0) { /* child process */
printf("%3d", i);
exit(0);
}
}
printf("\n");
exit(0);
}
void
sig_chld(int signo)
{
while (waitpid(-1, NULL, WNOHANG) > 0)
/* nothing to do */;
return;
}
void
err_sys(const char *p_error)
{
perror(p_error);
exit(1);
}