EmbLogic's Blog

signals

void signal_handler( int sig)
{
if(sig==SIGINT)
{
printf(“Signal %d received\n”,sig);
(void) signal(SIGINT,SIG_IGN);
(void) signal(SIGINT,SIG_DFL);
}………….

in the above code when we press ctrl+c for the second time signal terminates the process instead of being ignored…is there any error in the above code??

2 Responses to signals

  1. yashvardhan says:

    Hi Akash,
    No, there is nothing wrong with the code. It is the expected behaviour.
    See when you will press ctrl + c for the first time, it will call your signal handler, and it will print the first statement and then the execution moves to next statement and there you are re-registering the behavior with the kernel for SIGINT (SIG_IGN)and then it will move to the next statement where you have registered it again. So your current handler is SIG_DFL which is registered with the kernel. So when you press ctrl+c for the second time, current handler is SIG_DFL, so the process will be terminated NOT IGNORED.

  2. hemant.kumar says:

    (void) signal(SIGINT,SIG_DFL);
    (void)signal(SIGINT,SIG_IGN);

    try this..

    here your signal(SIGINT) will be ignored ..
    becuse you have declared it after the SIG_DFL

Leave a Reply to hemant.kumar Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>