//------------------------------------------------------------------- // sigdemo.cpp // // This program demonstrates the basic Linux signal-handling // mechanism: it calls the 'sigaction()' library-function to // install our own signal-handling routine for any 'SIGSEGV' // signals (Segmentation Violation), then calls a sunroutine // that triggers the generation of just this type of signal. // You can use our 'run.cpp' program to view the exit-status // and thereby verify that exit was from the signal-handler. // // compile using: $ make sigdemo // execute using: $ run sigdemo // // programmer: ALLAN CRUSE // written on: 03 MAY 2005 //------------------------------------------------------------------- #include // for printf() #include // for exit() #include // for sigaction() void myhandler( int signum, struct siginfo *si, void *appcontext ) { printf( "signal %d was caught!\n", signum ); exit( 1 ); } void myfunction( char *ptr ) { // write 0 to the specified memory-address *ptr = 0; } int main( int argc, char **argv ) { // declares a 'sigaction' structure struct sigaction sa = {0}; // initializes 'sigaction' structure sigemptyset( &sa.sa_mask ); sa.sa_flags = SA_SIGINFO; sa.sa_sigaction = myhandler; // installs our handler for SIGSEGV signals sigaction( SIGSEGV, &sa, NULL ); // calls a function that will use an invalid address myfunction( NULL ); // returns exit-status 0 (assuming we ever get here) exit( 0 ); }