#include #include #include #include #include #include #define MAXS 256 void sigPrintf(const char *format, ...) { char buf[MAXS]; va_list args; va_start(args, format); /* reentrant */ vsnprintf(buf, sizeof(buf), format, args); /* reentrant */ va_end(args); /* reentrant */ write(1, buf, strlen(buf)); /* async-signal-safe */ } void errExit(char * s) { perror(s); exit(1); } static void sigHandler(int sig) { static int count = 0; if (sig == SIGINT) { count++; sigPrintf("Caught SIGINT (%d)\n", count); return; /* Resume execution at point of interruption */ } /* Must be SIGQUIT - print a message and terminate the process */ sigPrintf("Caught SIGQUIT - that's all folks!\n"); exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { int i = 0; /* Establish same handler for SIGINT and SIGQUIT */ if (signal(SIGINT, sigHandler) == SIG_ERR) errExit("signal"); if (signal(SIGQUIT, sigHandler) == SIG_ERR) errExit("signal"); //if (signal(SIGKILL, sigHandler) == SIG_ERR) errExit("signal"); for (;;) /* Loop forever, waiting for signals */ i = i + 1; //pause(); /* Block until a signal is caught */ }