Signal question

This is a discussion on Signal question within the Linux Programming forums, part of the Platform Specific Boards category; Hi I want to catch a signal and continue the program. But when I catch it, my signal handler takes ...

  1. #1
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269

    Signal question

    Hi

    I want to catch a signal and continue the program. But when I catch it, my signal handler takes the control and program terminates after the signal handler returns.

    Here is the code
    Code:
    struct itimerval it;
    
    static void sig_handler(int sig)
    {
    	printf("Signal\n");
    }
    
    int main()
    {
    	signal(SIGALRM,sig_handler);
    	
    	it.it_value.tv_sec=1;
    	it.it_value.tv_usec=0;
    
    	setitimer(ITIMER_REAL,&it,0);
    
    	sleep(10);	
    	return 0;
    }
    I want to print screen "Signal" continuously when a signal is caugth (so, print "Signal" once in a second).

    How can I do this?

    Thanks...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    My man page
    Code:
           Trying to change the semantics of this call using defines and includes  is  not  a  good
           idea. It is better to avoid signal altogether, and use sigaction(2) instead.
    sigaction() gives you much more control over what happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Hi

    I rewrite the code with sigaction, thanks for warning me about it.

    Actually the problem is using sleep() to waste time (APPLICATION USAGE section - http://www.opengroup.org/onlinepubs/009695399/functions/sigaction.html)! If I use some other method to keep the process running, the signal is caught every time. I also need to set timer each time a signal is caught. This code behaves as a want
    Code:
    struct itimerval it;
    
    long unsigned int fibonacci (unsigned int n)
    {
     .......
    }
    
    static void sig_handler(int sig)
    {
            printf("Signal\n");
            setitimer(ITIMER_REAL,&it,0);
    }
    
    int main()
    {
            struct sigaction sa;
    
            sa.sa_handler = sig_handler;
            sigemptyset(&sa.sa_mask);
            sa.sa_flags = SA_RESTART;
    
            sigaction(SIGALRM,&sa,NULL);
    
            it.it_value.tv_sec=1;
            it.it_value.tv_usec=0;
    
           setitimer(ITIMER_REAL,&it,0);
    
            fibonacci(45);
            return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. keyboard capturing
    By xlnk in forum Linux Programming
    Replies: 4
    Last Post: 01-31-2003, 12:02 PM
  5. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM

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