Devi installare l'handler del segnale prima di volerlo catturare.
codice:
// Compiled an tested with: i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)
#include <stdio.h>
#include <signal.h>
static unsigned int sPleaseExit = 0;
static void Terminator (int theSignal){
if (SIGINT == theSignal){
sPleaseExit = 1;
}
}
int main (int argc, const char * argv[]) {
if (SIG_ERR != signal (SIGINT, Terminator)){
int i;
for (i = 0; 0 == sPleaseExit; ++i){
// to exit from this loop, send me SIGINT signal (press CTRL-C)
printf ("i = %d;\n", i);
}
// now do whatever you want: write to file etc.
}
else{
printf ("signal error;\n");
}
printf ("bye bye...\n");
return 0;
}