E.9. Signal Handling
An unexpected event, or signal, can terminate a program prematurely. Some unexpected events include interrupts (pressing Ctrl+C on a UNIX, LINUX, Mac OS X or Windows system), illegal instructions, segmentation violations, termination orders from the operating system and floating-point exceptions (division by zero or multiplying large floating-point values). The signal-handling library provides function signal to trap unexpected events. Function signal receives two argumentsan integer signal number and a pointer to the signal-handling function. Signals can be generated by function raise, which takes an integer signal number as an argument. Figure E.5 summarizes the standard signals defined in header file . The next example demonstrates functions signal and raise.
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. |
Figure E.6 traps an interactive signal (SIGINT) with function signal. The program calls signal with SIGINT and a pointer to function signalHandler. (Remember that the name of a function is a pointer to the function.) Now, when a signal of type SIGINT occurs, function signalHandler is called, a message is printed and the user is given the option to continue normal execution of the program. If the user wishes to continue execution, the signal handler is reinitialized by calling signal again (some systems require the signal handler to be reinitialized), and control returns to the point in the program at which the signal was detected. In this program, function raise is used to simulate an interactive signal. A random number between 1 and 50 is chosen. If the number is 25, then raise is called to generate the signal. Normally, interactive signals are initiated outside the program. For example, pressing Ctrl+C during program execution on a UNIX, LINUX, Mac OS X or Windows system generates an interactive signal that terminates program execution. Signal handling can be used to trap the interactive signal and prevent the program from terminating.
Figure E.6. Using signal handling.
(This item is displayed on pages 1258 - 1260 in the print version)
1 // Fig. E.6: figE_06.cpp 2 // Using signal handling 3 #include 4 using std::cout; 5 using std::cin; 6 using std::endl; 7 8 #include 9 using std::setw; 10 11 #include 12 using std::raise; 13 using std::signal; 14 15 #include 16 using std::exit; 17 using std::rand; 18 using std::srand; 19 20 #include 21 using std::time; 22 23 void signalHandler( int ); 24 25 int main() 26 { 27 signal( SIGINT, signalHandler ); 28 srand( time( 0 ) ); 29 30 // create and output random numbers 31 for ( int i = 1; i <= 100; i++ ) 32 { 33 int x = 1 + rand() % 50; 34 35 if ( x == 25 ) 36 raise( SIGINT ); // raise SIGINT when x is 25 37 38 cout << setw( 4 ) << i; 39 40 if ( i % 10 == 0 ) 41 cout << endl; // output endl when i is a multiple of 10 42 } // end for 43 44 return 0; 45 } // end main 46 47 // handles signal 48 void signalHandler( int signalValue ) 49 { 50 cout << " Interrupt signal (" << signalValue 51 << ") received. " 52 << "Do you wish to continue (1 = yes or 2 = no)? "; 53 54 int response; 55 56 cin >> response; 57 58 // check for invalid responses 59 while ( response != 1 && response != 2 ) 60 { 61 cout << "(1 = yes or 2 = no)? "; 62 cin >> response; 63 } // end while 64 65 // determine if it is time to exit 66 if ( response != 1 ) 67 exit( EXIT_SUCCESS ); 68 69 // call signal and pass it SIGINT and address of signalHandler 70 signal( SIGINT, signalHandler ); 71 } // end function signalHandler
|