#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void graceful(int signum) { printf("\tChild confirming received signal: %i\n", signum); puts("\tChild about to terminate gracefully..."); sleep(1);
puts("\tChild terminating now..."); _exit(0);
}
void set_handler() { struct sigaction current;
sigemptyset(¤t.sa_mask);
current.sa_flags = 0;
current.sa_handler = graceful;
sigaction(SIGTERM, ¤t, NULL);
}
void child_code() { set_handler();
while (1) { sleep(1);
puts("\tChild just woke up, but going back to sleep."); }
}
void parent_code(pid_t cpid) { puts("Parent sleeping for a time..."); sleep(5);
if (-1 == kill(cpid, SIGTERM)) { perror("kill"); exit(-1);
}
wait(NULL);
puts("My child terminated, about to exit myself...");}
int main() { pid_t pid = fork();
if (pid < 0) { perror("fork"); return -1;
}
if (0 == pid)
child_code();
else
parent_code(pid);
return 0;
}