The program is very simple, I have a handler function named fintr()
and the program is:
void fintr(); int main() { int i; signal(SIGINT,fintr); while(1) { printf("*"); } return 0; } void fintr() { printf("signal received"); exit(1); }
Can I put signal(SIGINT,fintr);
at the end of the function main()
? And why do we have to put it in the beginning of main()
?
Advertisement
Answer
Putting it at the end means it will come after the while (1) ...
loop, so no, you can’t put it in the end, because it would never be executed.
Also, pay attention to the code you place inside signal handlers: It is not safe to call printf()
inside a signal handler.