Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Signal Handling

C++ Signal Handling

A signal is an interrupt delivered by the operating system to a process that terminates a program prematurely. On UNIX, LINUX, Mac OS X, or Windows systems, an interrupt can be generated by pressing Ctrl+C.

Some signals cannot be caught by a program, but the signals listed in the table below can be caught in a program and appropriate action can be taken based on the signal. These signals are defined in the C++ header file it.

Signal Description
SIGABRT Abnormal termination of the program, such as calling abort.
SIGFPE Erroneous arithmetic operation, such as division by zero or an operation that causes overflow.
SIGILL Detects an illegal instruction.
SIGINT Program termination (interrupt) signal.
SIGSEGV Illegal access to memory.
SIGTERM Termination request sent to the program.

signal() Function

The C++ signal handling library provides the signal function, used to catch unexpected events. The following is the syntax of the signal() function:



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

This looks a bit difficult; the following syntax format is easier to understand:

signal(registered signal, signal handler)

This function takes two parameters: the first parameter is the identifier of the signal to be set, and the second parameter is a pointer to the signal handling function. The return value is a pointer to the previously set signal handling function. If no signal handling function was previously set, the return value is SIG_DFL. If the previously set signal handling function was SIG_IGN, the return value is SIG_IGN.

Let us write a simple C++ program that uses the signal() function to catch the SIGINT signal. No matter what signal you want to catch in your program, you must use the signal function to register the signal and associate it with the signal handler. See the example below:

Example

#include

<

iostream

#include

<

csignal

#include

<

unistd.h

using

namespace

std

;

void

signalHandler

(

int

signum

)

{

cout

<<

"

Interrupt signal (

"

<<

signum

<<

"

) received.

\

n

"

;

// Clean up and close

// Terminate the program

exit

(

signum

)

;

}

int

main

(

)

{

// Register signal SIGINT and the signal handler

signal

(

SIGINT

,

signalHandler

)

;

while

(

1

)

{

cout

<<

"

Going to sleep....

"

<<

endl

;

sleep

(

1

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Going to sleep....

Going to sleep....

Going to sleep....

Now, press Ctrl+C to interrupt the program, and you will see the program catch the signal, print the following content, and exit:



Going to sleep....

Going to sleep....

Going to sleep....

Interrupt signal (2) received.

raise() Function

You can use the function raise() to generate a signal; this function takes an integer signal number as a parameter, with the following syntax:



int raise (signal sig);

Here,sig is the number of the signal to be sent; these signals include: SIGINT, SIGABRT, SIGFPE, SIGILL, SIGSEGV, SIGTERM, SIGHUP. The following is an example of using the raise() function to generate a signal internally:

Example

#include

<

iostream

#include

<

csignal

#include

<

unistd.h

using

namespace

std

;

void

signalHandler

(

int

signum

)

{

cout

<<

"

Interrupt signal (

"

<<

signum

<<

"

) received.

\

n

"

;

// Clean up and close

// Terminate the program

exit

(

signum

)

;

}

int

main

(

)

{

int

i

=

0

;

// Register signal SIGINT and the signal handler

signal

(

SIGINT

,

signalHandler

)

;

while

(

++

i

)

{

cout

<<

"

Going to sleep....

"

<<

endl

;

if

(

i

==

3

)

{

raise

(

SIGINT

)

;

}

sleep

(

1

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result and exits automatically:



Going to sleep....

Going to sleep....

Going to sleep....

Interrupt signal (2) received.