tags:

views:

64

answers:

2

Hi, just a quick question regarding C and processes. In my program, I create another child process and use a two-directional pipe to communicate between the child and parent. The child calls execl() to run yet another program.

My question is: I want the parent to wait n amount of seconds and then check if the program that the child has run has exited (and with what status). Something like waitpid() but if the child doesn't exit in n seconds, I'd like to do something different.

+2  A: 

You can use waitpid() with WNOHANG as option to poll, or register a signal handler for SIGCHLD.

Bastien Léonard
the waitpid() route seems to be the best. is there a way that i can use waitpid() to get the exit value of the last child process? and i'm assuming that SIGCHLD runs the callback when a child exits?
Gary
+1  A: 

You can use an alarm to interrupt waitpid() after N seconds (don't use this approach in a multithreaded environment though)

   signal(SIGALRM,my_dummy_handler);
   alarm(10);

   pid_t p = waitpid(...);
   if(p == -1) {
     if(errno == EINTR) {
        //timeout occured
      } else {
        //handle other error
     }
nos
This seems to be what I'm after :) Only problem is that if the process doesn't end, the code after waitpid() never gets run. How can I fix this?
Gary
or if that doesn't make sense, i want to return process control to the code after waitpid() so that it can kill the offending function
Gary