signal handling

This is a discussion on signal handling within the C Programming forums, part of the General Programming Boards category; here is my first attempt at coding a signal handler... Code: #include <stdio.h> #include <stdlib.h> #include <signal.h> void SigHandler( int ...

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    signal handling

    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
    to boldy code where...

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Most of the system calls in UNIX are interrupted when the thread where they are called receive a Signal (Ofcourse, most of them, if not all). Also, if you haven't handled signals properly, the result could be unpredictable (as in your case). When I say 'not handled properly', I mean that when scanf() is waiting for an input from the user, and the user pressed the [CTRL+C] (SIGINT), you have not taken care of this situation.
    See help on sigignore() and sighold() to overcome your problem.

  3. #3
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46
    Originally posted by Salem
    Couple of points

    1. Which OS are you using?

    2. Try implementing your signal handler without any calls to printf/scanf. Running inside a signal handler is not exactly the same as running in your program.
    I'm using Mac OS 8.6, but my code had the same results
    when compiled on a Win 2000 workstation.

    trk
    to boldy code where...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 01:36 PM
  3. Signal Handling
    By DarrenY in forum C Programming
    Replies: 4
    Last Post: 06-04-2006, 11:30 AM
  4. Signal Handling - Are they part of ANSI C?
    By Stanley S in forum C Programming
    Replies: 3
    Last Post: 12-21-2005, 06:49 AM
  5. error signal handling
    By bizzu in forum C Programming
    Replies: 5
    Last Post: 04-29-2002, 03:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21