C Program to register signal handler for SIGTERM and when it receives the signal, the program should print some information about the origin of the signal

struct sigaction act;
void sighandler(int signum, siginfo_t *info, void *ptr)
printf("Received signal %d\n", signum);
printf("Signal originates from process %lu\n",
(unsigned long)info->si_pid);
int main()
printf("I am %lu\n", (unsigned long)getpid());
memset(&act, 0, sizeof(act));
act.sa_sigaction = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &act, NULL);
// Waiting for CTRL+C...
return 0;
I am 4942
Received signal 15
Signal originates from process 4924

Leave a Reply