Signals are interrupts of software delivered to a process by the Operating System. The OS can also issue signals based on system or error conditions. Programmers can generate interrupts by pressing Ctrl + C on a UNIX, LINUX, Mac OS X, or Windows system. There is some default behavior for some. This tutorial will teach you how to handle and manage the signals correctly.

Some signals are there that the program cannot catch, but there is a list of signals that programmers can in the program and take adequate actions based on the signal. The signals are defined in <csignal> header file. Here are lists of signals along with their description and working capability:

Signal Description
SIGABRT It is used for abnormal termination of the program, such as a call to abort.
SIGFPE An erroneous arithmetic operation, such as dividing by zero or an operation resulting in overflow.
SIGILL It is used to detect an invalid instruction.
SIGINT It is used to receive interactive program interrupt signals.
SIGSEGV Invalid access to storage.
SIGTERM A termination request sent to the program.
SIGHUP Hang-up (POSIX). It reports that the user's terminal is disconnected. The signal is used to notify the termination of the controlling process.
SIGQUIT It is used to terminate the process and generate a core dump.
SIGTRAP Trace trap
SIGBUS BUS error, which indicates access to an invalid address.
SIGUSR1 User-defined signal 1
SIGUSR2 User-defined signal 2
SIGALRM Alarm clock, which indicates the expiration of a timer. It is used by the alarm() function.
SIGTERM Used for termination. This signal can be blocked, handled, and ignored. Generated by kill command.
SIGCONT This signal is sent for processing to make it continue.
SIGSTOP Stop, unblockable. This signal is used to stop a process. This signal cannot be handled, ignored, or blocked.

The signal() function

The signal-handling library of C++ provides a function signal to trap unexpected events. The syntax of the signal function is:

Syntax:

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

The signal() function is set to handle a signal. It specifies a way to handle the signals with the signal number specified by sig. Parameter func specifies one of the three ways in which a program can handle a signal:

  • Default handling (SIG_DFL): The signal is handled by the default action for that particular signal.
  • Ignore signal (SIG_IGN): The signal is ignored, and the code execution will continue even if not meaningful.
  • Function handler: A specific function is defined to handle the signal.

Here is a simple C++ program where we will catch the SIGINT signal using the signal() function.

Example:

#include <iostream>
#include <csignal>

using namespace std;

sig_atomic_t signaled = 0;

void my_handler (int param)
{
    signaled = 1;
}

int main ()
{
    void (*prev_handler)(int);
    // register signal SIGINT with handler 
    prev_handler = signal (SIGINT, my_handler); 
    raise(SIGINT);
    cout<< "signaled is: " << signaled;
}