infinite loop with signal chaining

This is a discussion on infinite loop with signal chaining within the C Programming forums, part of the General Programming Boards category; HI I'm trying to play around with some signals. When I try to catch the FPE signal and chain it ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    57

    infinite loop with signal chaining

    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;
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    57
    Well I know one thing I did wrong now, cant recover after a FPE so should of ended the program there.

    Doesnt explain the looping though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 03:45 AM
  3. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. infinite loop
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-22-2001, 11:04 AM

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