0

I want the program to test the trapping of the signal by typing CTRL+C to generate a signal of type SIGINT. I don't know, my program just counts to the first interrupt signal and ends the program (just jumps straight into the INThandler function)

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>

void signalHandler( int signalValue ); /* prototype */
void  INThandler(int signalValue);


int main( void )
{
 int i; /* counter used to loop 100 times */
 int x; /* variable to hold random values between 1-50 */

 signal( SIGUSR1, signalHandler ); 
 signal(SIGUSR1, INThandler);
 srand( time( NULL ) );


    for ( i = 1; i <= 100; i++ ) {
        x = 1 + rand() % 50;

        if ( x == 25 ) {
            raise( SIGUSR1 );
        } 
        printf( "%4d", i );


        if ( i % 10 == 0 ) {
            printf( "\n" );
        } 
    }
  return 0; 
} 


void signalHandler( int signalValue )
{
  int response; 

  printf( "%s%d%s\n%s","\nInterrupt signal ( ", signalValue, " ) received.",
                     "Do you wish to continue ( 1 = yes or 2 = no )? \n" );

   scanf("%d", &response);
    if ( response == 1 ) {
        signal( SIGINT, signalHandler );
    }
    else {
    signal(SIGINT, INThandler);
    } 

}


void  INThandler(int signalValue)
{
  signal(signalValue, SIG_IGN);
  printf("\nCtrl-C command detected!");
  exit(0);

}
  • First you set `signalHandler` to handle `SIGUSR1`, then you set `INThandler` to handle it instead. What do you expect to happen? – Biffen Dec 04 '14 at 15:27

2 Answers2

0

Your code currently says:

signal(SIGUSR1, signalHandler); 
signal(SIGUSR1, INThandler);

The first call to signal() is effectively ignored. You've installed INThandler as the signal handler for the SIGUSR1 signals your program sends itself, and you have not installed any signal handler for SIGINT.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • can't believe that I did such a stupid mistake, now it works.. thank you! –  Dec 04 '14 at 15:32
  • See also [How to avoid using `printf()` in a signal handler?](http://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler/16891799#16891799) for information about what you can and can't do in a signal handler. Your signals are raised synchronously, not asynchronously, which reduces the risks involved drastically. – Jonathan Leffler Dec 04 '14 at 15:40
0

Not sure what you are doing with SIGUSR1. If you want to setup a handler to trap SIGINT, just setup the signal handler with signal( SIGINT, signalHandler ).

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

void signalHandler( int signalValue ); /* prototype */
void  INThandler(int signalValue);


int main( void )
{
    int i; /* counter used to loop 100 times */
    int x; /* variable to hold random values between 1-50 */

    signal( SIGINT, signalHandler ); 
    srand( time( NULL ) );


    for ( i = 1; i <= 100; i++ ) {
        x = 1 + rand() % 50;

        if ( x == 25 ) {
            raise( SIGUSR1 );
        } 
        printf( "%4d", i );


        if ( i % 10 == 0 ) {
            printf( "\n" );
        } 
        fflush( stdout );
        sleep( 1 );
    }
  return 0; 
} 


void signalHandler( int signalValue )
{
    signal( SIGINT, signalHandler );
    int response; 

    printf( "%s%d%s\n%s","\nInterrupt signal ( ", signalValue, " ) received.",
                     "Do you wish to continue ( 1 = yes or 2 = no )? \n" );

    scanf("%d", &response);
    if ( response != 1 ) {
        signal(SIGINT, INThandler);
    } 
}


void  INThandler(int signalValue)
{
    signal(signalValue, SIG_IGN);
    printf("\nCtrl-C command detected!");
    exit(0);
}
1   2   3^C
Interrupt signal ( 2 ) received.
Do you wish to continue ( 1 = yes or 2 = no )? 
1
   4   5   6   7^C
Interrupt signal ( 2 ) received.
Do you wish to continue ( 1 = yes or 2 = no )? 
1
   8   9  10
  11  12^C
Interrupt signal ( 2 ) received.
Do you wish to continue ( 1 = yes or 2 = no )? 
1
  13  14  15  16  17^C
Interrupt signal ( 2 ) received.
Do you wish to continue ( 1 = yes or 2 = no )? 

2
  18  19  20
  21  22^C
Ctrl-C command detected!---
George Houpis
  • 1,729
  • 1
  • 9
  • 5