Introduction to C++ | Signal Handling

Signal Handling in C++

Signal handling in C++ is a method of responding to asynchronous events or signals during program execution. A signal is an interrupt that informs a program that an event has occurred, such as a division by zero, an invalid memory reference, or even a user-defined event. Signals allow you to handle exceptional conditions and perform cleanup or error-handling routines before the program terminates.

What is a Signal?

A signal is a notification sent to a process by the operating system or another process to indicate an event. The event could be a hardware error, software exception, or a user-defined signal.

How to Handle Signals?

Signal handling in C++ is typically done using the signal() function from the signal.h library. The signal() function allows you to associate a signal with a custom handler function.

Syntax

signal() Function Syntax


        #include <csignal>
        
        void signal_handler(int signal_num) {
            // Handle the signal
        }
        
        int main() {
            signal(SIGINT, signal_handler); // Register signal handler for SIGINT
            // Rest of the program
        }
                    

Example of Signal Handling

In this example, we will handle the SIGINT signal, which is sent when a user presses Ctrl+C.

Code Example


        #include <iostream>
        #include <csignal>
        
        using namespace std;
        
        // Signal handler function
        void signal_handler(int signal_num) {
            cout << "Signal " << signal_num << " received. Program will exit now!" << endl;
            exit(signal_num);  // Exit the program after handling the signal
        }
        
        int main() {
            // Register the signal handler for SIGINT (Ctrl+C)
            signal(SIGINT, signal_handler);
        
            cout << "Program is running... Press Ctrl+C to trigger the signal." << endl;
            
            while (true) {
                // Infinite loop to keep the program running until SIGINT is received
            }
        
            return 0;
        }
                    

Output

Program is running... Press Ctrl+C to trigger the signal.
(Press Ctrl+C)
Signal 2 received. Program will exit now!

Predefined Signals

Here are some common predefined signals that can be handled in C++:

Example of Handling Different Signals

In this example, we will handle multiple signals like SIGSEGV (Segmentation fault) and SIGFPE (Floating-point exception).

Code Example: Handling Multiple Signals


        #include <iostream>
        #include <csignal>
        
        using namespace std;
        
        // Signal handler function
        void signal_handler(int signal_num) {
            if (signal_num == SIGSEGV) {
                cout << "Segmentation fault occurred! Program will exit." << endl;
            } else if (signal_num == SIGFPE) {
                cout << "Floating-point exception occurred! Program will exit." << endl;
            }
            exit(signal_num);  // Exit the program after handling the signal
        }
        
        int main() {
            // Register signal handlers
            signal(SIGSEGV, signal_handler);
            signal(SIGFPE, signal_handler);
        
            cout << "Program is running... Triggering signals now." << endl;
            
            // Trigger a segmentation fault by dereferencing a null pointer
            int* ptr = nullptr;
            *ptr = 10;
        
            return 0;
        }
                    

Output

Program is running... Triggering signals now.
Segmentation fault occurred! Program will exit.

Important Points About Signal Handling

Pro Tip:

💡 Pro Tip

Handling signals in C++ can be useful for cleanup tasks, error handling, or even catching user interrupts. However, ensure that your signal handlers are as simple as possible to avoid unpredictable behavior, and do not rely on non-reentrant functions inside signal handlers.