Your question was a little bit unclear to me, but I will mention some things that I've thought about doing for similar situations.
When you create a process you could temporarily block SIGCHLD
and place a record of the child process (and how it is to be treated) into a table.
child_table_node_t * node = malloc(sizeof(child_table_node_t) );
// err check that
child_table_node_init(node, type_of_child_stuff);
sigset_t old, set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, &old);
pid_t pid = fork();
if (pid > 0) {
node->pid = pid;
add_to_table(alive_children, node);
sigprocmask(SIG_SETMASK, &old, NULL);
// other stuff
} else {
sigprocmask(SIG_SETMASK, &old, NULL);
if (!pid) {
// do child stuff
} else {
// do parent with no child stuff
}
}
Your handle_sigchild
can then look up children who die (or stop or whatever) in the table and do something for them. I suggest removing them from the alive_children
table and putting them in a dead_children
table in the signal handler when they die so that your application can more easily handle them and deallocate their memory. If you want to keep up with children being stopped as well as dying then that will get a little bit more complicated b/c you won't want to remove them from the alive table. It might be easier to just traverse the table in the non-signal code to locate changed nodes. If that were the case you might be able to do that without blocking the signals except when you added or removed a node.
Your application could then do actions specific to the child that died based on data stored in the table node for that process. If the child were in the foreground then it's death (or stop action) would be to store the exit code in to the exit code variable and start taking commands from the terminal or script again. If the newly deceased child were a background process then you just record its exit code and report that to the terminal before the next time you print a command prompt (if you are in interactive mode).