Consider the following code:
#include <signal.h>
#include <stdio.h>
void catch ()
{
printf("hi\n");
}
int main()
{
struct sigaction act;
act.sa_handler = catch;
sigaction(SIGINT, &act, NULL);
for(;;);
return 0;
}
When this program is run. The first time I press CTRL-C it prints "hi".
But the second time the program exits. what could be the reason for this?
What I wanted was that the program catch the signal each time it is raised.
If you don't use any SA_FLAG to explicitly define the behavior of "what to do after first catch of signal", it should be work.
Clear the contents of the sigaction, then initialize it.
See, sigaction(2).
In addition, do not use printf inside your signal handlers as Daniel pointed out. See signal-safety(7)
If you want to print something, or simply do something in your signal handler, you must use signal-safe functions. In your case, instead of using printf, you can use write() system call. See write(2).
By,