Hi All,
in the following program I have observed that the program is not jumped on the handler but if i remove the statement sleep(1); it work fine.
the piece of the code is
…
while(1)
{
if (flag)
printf(“waiting for signal”);
else
printf(“signal invoked”);
sleep(1);
}
my question is when we press Ctrl+C (generating a signal) in such case the program should moved to signal handler function but in this case why it is not working correctly while having sleep(1); statement there why it is so?
I too faced this problem.
It may sound weird but for unknown reasons, adding a newline character ‘\n’ at the end of every call to printf function solves the problem.
so change your printf calls to include a newline character at the end.
It works.
Use comma operator after printf(). i.e.
printf(“signal invoked”),sleep(1);
Use comma operator after printf(). i.e.
printf(“signal invoked”),sleep(1);
it will work fine