While going through signals, I found that statement inside the handler was not printed. Here is the code I used:
#include"stdio.h"
#include"signal.h"
#include"unistd.h"
void handlerSIGINT(int sig)
{
if(sig == SIGINT)
printf("\nFinally caught SIGINT...");
}
int main()
{
printf("Hello ... soon u'll receive a signal");
if(signal(SIGINT, handlerSIGINT) == SIG_ERR)
{
printf("error in SIGINT handling");
}
while(1)
sleep(1);
/*now press control + C to see the effect */
return 0;
}
I got the following output when I run the program :
[root@node1 mytest]# ./a.out
^CHello ... soon u'll receive a signal
^CFinally caught SIGINT...
^CFinally caught SIGINT...
^CFinally caught SIGINT...
^Z
[1]+ Stopped ./a.out
My confusion is : When I first pressed "Ctrl+C" it didn't print the message in the handler i.e. "Finally caught SIGINT..." .But from the second time it started printing the message. Can anyone explain why is this happening...