I am forking n number of children and saving each child's pid_t in a dynamically allocated table (yes i want to do that since I need the parent process to own these id's). My program should expect a SIGCHLD signal and when it recieves it, it must replace the child terminated. So, now i must change the array index of child with pid_t and replace it with a newly forked one.
My question is, how can I actually do such a thing, since the handler will be called "outside" of my main and will not have access to my table of pid's. Maybe make the table global and only initialize it for parent? Or can handler interact with main and in some way pass the pid value that is free'ed and replaced?
And I for one might ask, should I even create such a table in the first place. Should I just work with the returning values of fork instead of the table that parent has? In this case I will have to make an if statement checking every pid=fork() instead of just having one if that will enter for just parentid and which will have a loop in it for every child's id in the table.
EDIT: I don't see where my code would help since I think i explained the process, but there is what i described.
signal(SIGCHLD,handler)
pid_t table[n];
for(int i=0;i<n;i++){
pid_t pid;
if((pid=fork())!=0){
table[i]=pid;
}
}
void handler(int num){
//what do i do here?
}