Thread: signal handler does not print

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    signal handler does not print

    Dear all:

    How come the program below returns alarm clock but does not print "signal sent!!"?What did I do wrong?

    Thanks


    Code:
    /* Using a signal handler */
    
    #include <signal.h>
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <unistd.h>
    
    sig_atomic_t sigusr1_count = 1;
    
    void handler (int signal_number) {
    	printf("signal sent!!\n");
    }
    
    int main () {
    	int i;
    	struct sigaction sa;
    	memset (&sa, 0, sizeof(sa));
    	sa.sa_handler = handler;
    	sigaction (SIGUSR1, &sa, NULL);
    
    	alarm(1);
    	//kill (getpid(), SIGUSR1);
    	while (1) {
    		printf("Hello\n");
    	}
    
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your handler is listening for SIGUSR1, but alarm sends SIGALRM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    alarm
    Try "SIGALRM".

    sigaction
    Note the example code. That may not be the same as memset'ing to 0.

    gg

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    37
    I think I misunderstand the usage of alarm(), is there a way I can make SIGUSR1 to behave like SIGALRM?

    Thanks.


    Quote Originally Posted by Codeplug View Post
    alarm
    Try "SIGALRM".

    sigaction
    Note the example code. That may not be the same as memset'ing to 0.

    gg

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Install a handler for SIGALRM and have that handler call kill(getpid(), SIGUSR1);

    EDIT: This could potentially get you into some serious race conditions if you have your SIGUSR1 handler reset the alarm in return. Also, be careful in using sleep, usleep or the setitimer/getitimer functions as they may conflict with alarm().
    Last edited by anduril462; 07-27-2011 at 11:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signal handler for assert()
    By jeffcobb in forum Linux Programming
    Replies: 17
    Last Post: 06-10-2010, 08:36 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Signal handler function - pointer to this gets lost
    By maus in forum C++ Programming
    Replies: 1
    Last Post: 07-01-2009, 09:10 AM
  4. using a signal handler to kill processes
    By dinjas in forum C Programming
    Replies: 2
    Last Post: 03-16-2005, 12:58 PM
  5. signal handler
    By falconetti in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 07:54 PM