views:

232

answers:

1

Hi!

we are programming a bash like shell in C but we have problems with the background processes. The thing is that the father waits for the children process when there is no &, we have made a signal_handler for SIGCHLD which have a wait inside.

The main problem that we want to avoid is: the signal_handler executes always (with background and foreground processes), so if the signal_handler catches the SIGCHLD before the father process, the father's wait has not children to wait for, so it returns a error.

We depend on who of them executes before, and we want that if there isn't & the signal_handler wait doesn't collect the children before the father does.

Any help could be of paramount importance. Thanks a lot in advance.

+1  A: 

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).

nategoose
Your suggestions were very useful. We finally made an array to store the pid of the background processes in order to wait only for these PIDs in the signal_handler.We really thank you your answer ;).
PID
I'm glad it was helpful. Good luck with your project :-)
nategoose