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

}

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *