C++ Signal Handling


What is signal handling?

  • Signals are the interrupts which force the operating system to stop the ongoing tasks and attend the task for which the interrupt has been sent.
  • These interrupt can pause a service in any program of an operating system. In C++, various signals are offered which can catch and process in a program.
  • The programmers can generate interrupts by pressing Ctrl+C on a LINUX, UNIX, Windows or Mac OS X. Some of the signals cannot be caught by the program but there is a following list of signals which you can catch in your program and can take proper actions based on the signal.

The <csignal> header file is used to define these signals.


It produces receipt for an active signal.


It terminates the program abnormally.


It sends a termination request to the program.


It produces the erroneous arithmetic operation such as divide by zero or an operation resulting in an overflow.


It means an invalid access to storage.


This signal detects the illegal command or instruction.

The signal ( ) Function

In C++, signal handling library provides function signal to trap unexpected events.

Syntax for this function is:

signal(registered signal, signal handler)

  • In the above syntax, the first argument is an integer which represents the signal number and the second argument is the pointer to a signal handling function.
  • The signal which we want to be catching it should be registered and must be associated with a signal handling function. It should be of void type.

Let us have a look at the example:

To interrupt in between the program press Ctrl+C.

The raise( ) Function

In C++, the raise( ) function is used to generate signals. It takes integer signal number as an argument.

Syntax for the following is:

raise(signal sig);

In the above syntax, sig is the signal number which is used to send any of the signals: SIGABRT, SIGINT, SIGTERM, SIGSEGV, SIGILL, SIGHUP, SIGFPE.

Let us have a look at the example:


Like it? Please Spread the word!