[clug-talk] Call a program in C and get the child PID?

Mark Carlson carlsonmark at gmail.com
Mon Sep 19 11:47:37 PDT 2005


On 9/19/05, Robert Lewko <lewkor at telus.net> wrote:
> The following code does not implement the signal handler.  I cannot spare the
> time to look up how to do the signal handler just now.  What you need to
> lookup is how to set up a signal handler (use the sigaction function call)
> and just call waitpit(pid) in the signal handler.
> 

The signal handler code is also in Advanced Linux Programming Ch. 3 page 60 ( 
http://www.advancedlinuxprogramming.com/alp-folder/alp-ch03-processes.pdf )

#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

sig_atomic_t child_exit_status;

void clean_up_child_process (int signal_number)
{
  /* Clean up the child process.  */
  int status;
  wait (&status);
  /* Store its exit status in a global variable.  */
  child_exit_status = status;
}

int main ()
{
  /* Handle SIGCHLD by calling clean_up_child_process.  */
  struct sigaction sigchld_action;
  memset (&sigchld_action, 0, sizeof (sigchld_action));
  sigchld_action.sa_handler = &clean_up_child_process;
  sigaction (SIGCHLD, &sigchld_action, NULL);
 
  int i,pid;
  /* Now do things, including forking a child process.  */
  /* ... */
  return 0;
}



More information about the clug-talk mailing list