/*-------------------------------------------------------------------------- Arrgh. Red Hat Linux 4.2 always seems to restart read() if interrupted by a signal. RH5.0 can be coaxed into making the read() return when interrupted by a signal, thank goodness. http://www.dejanews.com/getdoc.xp?AN=313705083 has some relevant discussion. dank@alumni.caltech.edu --------------------------------------------------------------------------*/ #include #include #include static void alarm_handler( sig ) int sig; { printf("Got signal\n"); } main() { int i; /* Four different ways of setting up the signal handler. */ #if 0 (void) signal( SIGALRM, alarm_handler ); /* doesn't interrupt fgets() :-( */ #endif #if 1 (void) signal( SIGALRM, alarm_handler ); siginterrupt(SIGALRM, 1); /* works ok in RH5.0, not 4.2 */ #endif #if 0 (void) sysv_signal( SIGALRM, alarm_handler ); /* Doesn't link in RH4.2 or 5.0 */ #endif #if 0 struct sigaction newact; memset(&newact, 0, sizeof(newact)); newact.sa_handler = alarm_handler; sigemptyset(&newact.sa_mask); sigaddset(&newact.sa_mask, SIGALRM); sigaction(SIGALRM, &newact, NULL); /* works ok in RH5.0, not 4.2 */ #endif printf("Want to see 'Calling alarm...' then 'Got Signal' printed out three times \n\ in three seconds. If it just sits for longer than a few seconds, \n\ the fgets() didn't get interrupted, and the test is a failure.\n"); for (i=0; i<3; i++) { char buf[128]; printf("Calling alarm...\n"); alarm(1); gets(buf); /* Want this to return when alarm goes off */ } if (i == 3) printf("Test passed - alarm() successfully used to set a timeout on gets()\n"); else printf("Test failed - signal handler didn't stay active after first firing\n"); }