here is my first attempt at coding a signal handler...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void
SigHandler( int sig );
int main( void )
{
int choice = 0;
signal( SIGINT, SigHandler );
signal( SIGABRT, SigHandler );
printf( "Please choose signal ( 1 - SIGINT, 2 - SIGABRT ) : " );
scanf( "%d", &choice );
if( choice == 1 )
raise( SIGINT );
else if( choice == 2 )
raise( SIGABRT );/*abort();*/
printf( "\nI made it through here!\n" );
return 0;
}
void
SigHandler( int sig )
{
int choice = 0;
switch( sig ) {
case ( SIGINT ):
printf( "\nInterrupt signal received\n" );
break;
case ( SIGABRT ):
printf( "\nAbnormal termination signal received\n" );
break;
default:
printf( "\nUnhandled Signal\n" );
return;
}
printf( "Do you wish to continue ? ( 0 - No, 1 - Yes ) : " );
scanf( "%d", &choice );
if( choice ) {
signal( SIGINT, SigHandler );
signal( SIGABRT, SigHandler );
}
else
exit( EXIT_SUCCESS );
return;
}
I've tried to simulate two signals as user choices since this is
a test program.The problem is that when i use raise() the
signal is handled and the flow of control ( if choose so )
continues and executes that last printf(), but when i press
ctrl^c or i use abort() although the signal handler is invoked,
the program terminates no matter what i choose.
The truth is that i haven't found any real-world signal handler
example or any extensive coverage of signal handling methods.
Please suggest any a book or an internet resource that might
help and covers the subject thoroughly.
Any comment on the above code is welcome,
trekker