HI
I'm trying to play around with some signals. When I try to catch the FPE signal and chain it with another I get a infinite loop of them being called repeatedly. I'm not sure why. I'm also not sure why I cant see my alarm.
Any advice?
Thanks
Code:#include <stdio.h> #include <stdlib.h> #include <signal.h> #include <unistd.h> /* * Old handlers */ void (*old_sigfpe_handler)(int); void (*old_sigint_handler)(int); int sigint_count = 0; /* * New handlers */ void another_sigfpe_handler(int sig) { fprintf(stderr, "I got the signal first!\n"); old_sigfpe_handler(sig); } void handler(int sig) { void *retval; switch(sig) { case SIGINT: if(sigint_count < 1) { fprintf(stderr, "You pressed Control-C\n "); fprintf(stderr, "I wont end until you press"); fprintf(stderr, "it 3 times\n"); } else { retval = signal(SIGINT, old_sigint_handler); if(retval == SIG_ERR) perror("Error setting SIGINT handler"); } ++sigint_count; break; case SIGFPE: fprintf(stderr, "Don't divide by zero.\n"); alarm(5); break; case SIGUSR1: fprintf(stderr, "Somebody signalled me!\n"); break; case SIGALRM: fprintf(stderr, "You divided by zero 5 seconds ago\n"); break; case SIGSEGV: fprintf(stderr, "You can't dereference null pointer\n"); exit(1); } } /* * Program */ int main() { void *old_handler; int retval; int a, b; /* Install new sig handlers */ old_sigint_handler = signal(SIGINT, handler); if(old_sigint_handler == SIG_ERR) { perror("Error setting new SIGINT handler"); return 1; } old_handler = signal(SIGFPE, handler); if(old_sigfpe_handler == SIG_ERR) { perror("Error setting first new SIGFPE handler"); return 1; } old_handler = signal(SIGUSR1, handler); if(old_handler == SIG_ERR) { perror("Error setting new SIGUSR1 handler"); return 1; } old_handler = signal(SIGALRM, handler); if(old_handler == SIG_ERR) { perror("Error setting new SIGALRM handler"); return 1; } old_handler = signal(SIGSEGV, handler); if(old_handler == SIG_ERR) { perror("Error setting new SIGSEGV handler"); return 1; } old_sigfpe_handler = signal(SIGFPE, another_sigfpe_handler); if(old_sigfpe_handler == SIG_ERR) { perror("Error setting second new SIGFPE handler"); return 1; } /* Main user loop */ fprintf(stderr, "Read to go\n\n"); while(42) { retval = scanf("%i %i", &a, &b); if(retval < 2) { fprintf(stderr, "return value = %i\n", retval); } else { if(a == 1 && b == 1) retval = *((int *)0); else fprintf(stderr, "q = %i\n", a/b); } } /* Profit! */ return 0; }



LinkBack URL
About LinkBacks


