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??
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.
(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