Did you ever wonder what happens when an user hits ctrl-c? A SIGINT signal is generated and sent to the process running. This is the way to terminate programs from terminal.

When the signal occurs, the process has to tell the kernel what to do with it. Your process can ignore signal, catch signal or let the default action apply.

Note: SIGKILL and SIGSTOP cannot be ignored.

If a process wishes to handle a signal then in the code, the process has to register a signal handling function to the kernel. The following is the prototype of a signal handling function :

void <signal handler function> (int sig)

You can specify in this function how you want to handle it.

To invoke above function when a signal occurs, you need to register this function to kernal. To get the signal handler function registered to the kernel, the signal handler function pointer is passed as second argument to the ‘signal’ function. The prototype of the signal function is :

void (*signal(int signo, void (*func )(int)))(int);

Where func is the signal handler function, we defined.

Below is a program to handle SIGTERM signal. Run this program and send a SIGTERM signal to this to see what happens.


void sig_handler(int signo)
if (signo == SIGTERM)
printf("received SIGTERM\n");

int main(void)
if (signal(SIGTERM, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGTERM\n");
// wait so that we can easily issue a signal to this process
return 0;

As told earlier that KILL and STOP cannot be handled. Change above program to handle these two signals to see how the ‘signal’ system call responds in that case.

PS: Apart from handling the standard signals, we can also have user defined signals that can be sent and handled.

Subscribe - To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.