/* corrected signals example in CDS Architecture (Unix) lecture, 2007 */ 

#include <stdio.h>  // printf() -- added to show what is happening 
#include <stdlib.h> // exit()
#include <sys/wait.h> // wait()

#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

void catch_stop(int sig) {
  printf("Handler 1 caught signal %d\n", sig);
  return;
}

//#define THIS_SIG SIGSTOP  /* doesn't get caught! */
//#define THIS_SIG SIGUSR1  /* will get caught */
#define THIS_SIG SIGTERM    /* will get caught */	

int main(int argc, char* argv[]){
  pid_t id = fork();
  printf("Hello from Process ID %d \n", getpid());
  if (id==0) {
    signal(THIS_SIG, catch_stop);
    pause();  // wait until a signal is delivered, then return
    printf("process ID %d: exiting\n", getpid());
    exit(0);
  } else {
    kill(id, THIS_SIG);
    wait(NULL);
    printf("process ID %d: child %d has exited\n",  getpid(), id);
  }
  return 0;
}


