Signal Handlers in C

This Tutorial explains Signal Handlers in C with examples.

Signals are mechanisms for communicating with and manipulating processes. Since signals are asynchronous that is they can’t be synchronised with anything in the execution of program. Though signals can either be ignored or responded with default action, we can, however, prepare a program to handle signals. A function that handles signals is called a signal handlers in C. When a signal occurs while executing the program, program is paused, signal handler function for that signal is called and program can’t be resumed until handler function has completed.

The mechanism is when a signal occurs for which handler has been installed, system first reinstates the default action for the signal. This change prevents an infinite loop if the signal recurs within the handler. Next the handler is called and signal number is passed as argument to handler function. Note here that every signal has a positive signal number. We use them in our program by their name.

Type of work handler function can perform is limited. If signal is asynchronous that is it’s not caused from within the program; handler shouldn’t call any library function other than signal because their results are undefined. Additionally, handler may not access any static data except to assign a value to static data of type ‘volatile sig_atomic_t‘ so that CPU can perform atomic operations. To be truly safe, all about a handler function can do is to set one of these variables and return. Then rest of the program examines the value of this variable to ensure if signal has occurred.

The access restrictions define minimal functionality that is guaranteed to work in handler function. Data type ‘sig_atomic_t‘ defines a type that CPU can access atomically that is in a single unit. What we mean by atomic operations is that if variable is non-atomic i.e. operation taking more than one unit to complete, it might be that signal occurred in the middle of steps to complete the operation and therefore undefined results. On the other hand, atomic operation completes in single unit. For ex. a 16-bit machine processes 16-bit integer in one unit while processes 32-bit integer in two units.

Remember, Standard states that a handler function can terminate the program by calling ‘exit()’ function. Excepting the SIGABRT signal, handler function can terminate the program by calling ‘abort()’ function. These are library functions and might not work properly when called from handlers of asynchronous signals.

advertisement
advertisement

Default Signal Handlers

Every signal has a default course of action. They are:

  • Term – It terminate the process.
  • Ign – It ignore the signals.
  • Cont – It unblocks the stopped process.
  • Stop – It stop the process.

Example of ‘handler()’ function for asynchronous and synchronous signals

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
/* asynchronous_sig.c -- handles asynchronous signal */
#include <signal.h>
#include <stdio.h>
 
void handler(int signal)
{
    if (signal == SIGINT)
        /* manage signal here */
}
 
int main(void)
{
    signal(SIGINT, handler);
 
    /* rest program code */
 
    return 0;
}
/* synchronous_sig.c -- handles synchronous signals */
#include <signal.h>
#include <stdio.h>
 
void handler(int signal)
{
    if (signal == SIGBUS)
        /* manage signal here */
 
    if (signal == SIGILL)
        /* manage signal here */
 
    if (signal == SIGSEGV)
        /* manage signal here */
}
 
int main(void)
{
    signal(SIGBUS, handler);
    signal(SIGILL, handler);
    signal(SIGSEGV, handler);
 
    /* rest program code */
 
    return 0;
}

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.