I have the following code. When I compile it with the gnu extensions (-std=gnu99), the program will catch 5 SIGINT before ending (which I would expect). When compiled without it (-std=c99) ends after the second (and only outputs one line).
What am I missing?
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
int int_stage = 0;
int got_signal = 0;
void sigint(int parameter)
{
(void)parameter;
got_signal = 1;
int_stage++;
}
int main()
{
signal(SIGINT,sigint);
while(1)
{
if (got_signal)
{
got_signal = 0;
puts("still alive");
if (int_stage >= 5) exit(1);
}
}
return 0;
}
Use
sigaction(2)rather thansignal(2).The Linux man page has this, in particular, in the Portability section:
Using
std=gnu99, you’re getting BSD semantics. Using-std=c99, you’re getting System V semantics. So the signal handler is “reinstalled” in one case (BSD), and the signal disposition is reset back to SIG_DFL in the other (System V).