/*
 * Example forking code
 */

#include <sys/types.h>
#include <unistd.h>
#include <signal.h>

/*
 * The main program reaps when it gets a SIGCHLD
 */
void reaper(int sig) {
	printf("In parent: wait3 started\n");
	wait3(NULL,0,NULL);
	printf("In parent: wait3 ended\n");
	signal(SIGCHLD,reaper);
}

main() {
	pid_t pid[5];
	int count;

	signal(SIGCHLD,reaper);
	
	for (count = 0; count < 5; count++) {
		pid[count] = fork();
		if (pid[count] == -1) {
			/* An error occurred in fork */
			perror("fork");
		} else if (pid[count] == 0) {
			/* This is in the child */
			printf("In child, count=%d\n",count);
			exit(0);
		} else {
			/* This is in the parent */
			printf("In parent, pid[%d]=%d\n",count,pid[count]);
		}
	}
	sleep(30);
}
