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.
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
/* 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.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Computer Science MCQs
- Buy C Books
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Buy Computer Science Books