#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// prototype
static void sig_handler(int);
int main() {
int i;
int parent_pid;
int child_pid;
int status;
if( signal(SIGHUP, sig_handler) == SIG_ERR)
printf("Parent: Unable to create handler for SIGHUP\n");
// Getting the parents process id
parent_pid = getpid();
// Spawn the child process and get the process id.
if ((child_pid = fork()) == 0) {
printf("Child: This is the child --> %d \n", child_pid);
printf("Child: Sending SIGHUP to parent \n");
kill(parent_pid, SIGHUP);
exit(0);
}
else {
printf("Parent: Waiting for child to exit\n");
wait(&status);
printf("Parent: Process completed\n");
}
}
// Signal Handler
static void sig_handler(int signo) {
printf("Signal is recieved!");
switch (signo) {
case SIGHUP: /* Incoming signal is SIGHUP signal */
printf("---> SIGHUP recieved");
break;
}
return;
}