lundi 13 avril 2015

Parent process does not wait for all children to exit using signal()


Vote count:

0




The program initially asks the user to input the number of child processes to create. After creating the children, the parent sleeps and waits for all its children to terminate via a signal handler function 'handle_child' registered with SIGCHLD.





#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

int to_kill;

void handle_child(int sig)
{
pid_t id = wait(NULL);
printf("Reaped child with PID = %d\n",id);
--to_kill;
if(to_kill == 0){
printf("~All children have been reaped successfully.. parent will now exit~\n");
exit(0);
}
}

int main()
{
pid_t id, parent_id = getpid();
int children, i;
signal(SIGCHLD, handle_child);
printf("Enter number of child processes to spawn: ");
scanf("%d",&children);
to_kill = children;
for(i = 0; i < children; ++i){
id = fork();
if(id == 0){
printf("New child with pid = %d\n",getpid());
sleep(2);
return 0;
}
sleep(1);
}
sleep(30);
return 0;
}



The problem I'm facing is that the parent often exits without reaping all of its children. Some of the times the program runs perfectly well, and at other times it ends abruptly. What is exactly happening here?



asked 26 secs ago







Parent process does not wait for all children to exit using signal()

Aucun commentaire:

Enregistrer un commentaire