#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

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

void handler(int x) {
  printf("end child: %d\n", x);
}

int
get_a_child (int ttl) {
  int pid;

  pid = fork ();
  printf ("pid: %d\n", pid);
  if (pid < 0)
    exit (1);

  if (pid > 0)
    return (pid);

  sleep (ttl);
  _exit (0);
}

int
main (void) {
  struct sigaction action;
  int pid;

  pid = get_a_child (3);

  action.sa_handler = handler;
  sigemptyset (&action.sa_mask);
  action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  sigaction (SIGCHLD, &action, NULL);

  pid = get_a_child (6);

  waitpid (0, NULL, 0);
  waitpid (0, NULL, 0);

  exit (0);
}
