C++ examples for Function:Useful Function
Signal | Explanation |
---|---|
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. |
#include <iostream> #include <iomanip> #include <csignal> #include <cstdlib> #include <ctime> using namespace std; void signalHandler( int ); int main() /*ww w.j av a 2 s . c o m*/ { signal( SIGINT , signalHandler ); srand( time( 0 ) ); // create and output random numbers for ( int i = 1; i <= 100; i++ ) { int x = 1 + rand() % 50; if ( x == 25 ) raise( SIGINT ); // raise SIGINT when x is 25 cout << setw( 4 ) << i; if ( i % 10 == 0 ) cout << endl; // output endl when i is a multiple of 10 } } // handles signal void signalHandler( int signalValue ) { cout << "\nInterrupt signal (" << signalValue << ") received.\n" << "Do you wish to continue (1 = yes or 2 = no)? "; int response; cin >> response; // check for invalid responses while ( response != 1 && response != 2 ) { cout << "(1 = yes or 2 = no)? "; cin >> response; } // determine if it is time to exit if ( response != 1 ) exit( EXIT_SUCCESS ); // call signal and pass it SIGINT and address of signalHandler signal( SIGINT , signalHandler ); }