I have a question about signal processing. Suppose that if we get a SIGINT signal, we should print “Received signal”. If within ten seconds the handler receives another signal, it should print “Shutdown” and then exit with status 1.
I made my code as follows:
#include <stdio.h> #include <signal.h> #include <unistd.h> void handler(int); void secondhandler(int); void alrmhandler(int); void alrmhandler (int alrmsig) { alarm(0); } void secondhandler(int sig) { /* after recieving second signal prints shutting down and exit */ printf("Shutting Down\n"); exit(1); } void handler ( int sig ) { /* recieve first SIGINT signal */ printf ("Recieved Signal\n"); /* handle for the alarm function */ signal(SIGALRM, alrmhandler); /* start 10s alarm */ alarm(10); /* catch second SIGINT signal within 10s*/ signal(SIGINT, secondhandler); } int main( void ) { signal(SIGINT, handler); printf( "Hello World!\n" ); for ( ;; ) { /* infinite loop */ } return 0; }
I tried to compile it with dev C ++, but that failed. Since SIGALRM is not declared (first used in this function).
In any case, I want to know if this code is correct. I am really not sure with alrmhandler (). Should I ignore SIGALRM?
source share