The code written below is to handle the received signals and reap the zombies by the parent process "from System Programming course of CMU".
Q1. what is the rule of -1 "the first arg of the waitpid function"? should not we pass the pid of the zombie we are reaping instead?
Q2. For the loop here, does it check all zombies each time a signal received by any precedent zombie?
int ccount = 0;
void child_handler2(int sig)
{
int child_status;
pid_t pid;
while ((pid = waitpid(-1, &child_status, WNOHANG)) > 0) {
ccount--;
safe_printf("Received signal %d from process %d\n",sig, pid);
}
}
void fork14()
have sent this signal
{
pid_t pid[N];
int i, child_status;
linux> ./forks 14
ccount = N;
signal(SIGCHLD, child_handler);
for (i = 0; i < N; i++)
if ((pid[i] = fork()) == 0) {
sleep(1); /* deschedule child */
exit(0); /* Child: Exit */
}
while (ccount > 0)
pause(); /* Suspend until signal occurs */
}