The signal() function allows you to register your own functions to be called when one of the following signals occur:
SIGABRT – Abnormal termination of the program, such as a call to abort
SIGFPE – An erroneous arithmetic operation, such as a divide by zero or an operation resulting in overflow
SIGILL – Detection of an illegal instruction
SIGINT – Receipt of an interactive attention signal
SIGSEGV – An invalid access to storage
SIGTERM – A termination request sent to the program
Good Resources
http://www.tutorialspoint.com/cplusplus/cpp_signal_handling.htm
Example
In your Initialize function:
//----- REGISTER EXTERNAL SIGNAL FUNCTION HANDLERS -----
//Functions to be called on these special signals being triggered
signal(SIGTERM, onterm);
signal(SIGABRT, onterm);
signal(SIGINT, onterm);
signal(SIGQUIT, onterm);
atexit(onexit);
The functions that will be called:
//*****************************************
//*****************************************
//********** ON TERMINATE SIGNAL **********
//*****************************************
//*****************************************
//This function is registered in initialise() to be called on various error signal events
static void onterm(int s)
{
exit(s != SIGHUP && s != SIGINT && s != SIGQUIT && s != SIGABRT && s != SIGTERM ? EXIT_SUCCESS : EXIT_FAILURE);
}
//************************************
//************************************
//********** ON EXIT SIGNAL **********
//************************************
//************************************
//This function is registered in initialise() to be called on the exit event
static void onexit(void)
{
//Nicely shut down and application specific things
}