/* include header files */

#include <stdio.h>       // printf
#include <stdlib.h>      // exit, atoi
#include <unistd.h>      // sleep, fork
#include <signal.h>      // kill
#include <sys/types.h>   // fork, kill
#include <sys/wait.h>    // wait

/* main function */

int main(int argc, char *argv[]){
  
  int status;
  int parent_sleep_time, child_sleep_time;

  int child_pid;
  int pid;
    
  if (argc!=3) {
    printf("Usage: %s parent_sleeptime child_sleeptime [in seconds]\n",argv[0]);
    return 0;
  }
  
  parent_sleep_time = atoi(argv[1]);
  child_sleep_time = atoi(argv[2]);
  
  child_pid = fork();
  
  if (child_pid == 0) { 
    
    /* child section */

    printf("Child: sleeping %d seconds \n",child_sleep_time);
    sleep(child_sleep_time);
    printf("Child: awaken!\n");
    
    exit(5);
  }
  
  else
    {
      /* parent section */

      printf("Parent: child %d created\n",child_pid);      
      printf("Parent: sleeping %d seconds\n",parent_sleep_time);
      
      sleep(parent_sleep_time);
      kill(child_pid,9);
      
      pid = wait(&status);  // suspent execution till status is available for a terminated child process
      
      printf("Parent: child %d completed with status %x\n",pid,status);  // printed in hexagonal
    }
      
  return 0;
}

