Back to index

glibc  2.9
sigh1.c
Go to the documentation of this file.
00001 #include <signal.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 
00005 /* This flag controls termination of the main loop. */
00006 volatile sig_atomic_t keep_going = 1;
00007 
00008 /* The signal handler just clears the flag and re-enables itself. */
00009 void 
00010 catch_alarm (int sig)
00011 {
00012   keep_going = 0;
00013   signal (sig, catch_alarm);
00014 }
00015 
00016 void 
00017 do_stuff (void)
00018 {
00019   puts ("Doing stuff while waiting for alarm....");
00020 }
00021 
00022 int
00023 main (void)
00024 {
00025   /* Establish a handler for SIGALRM signals. */
00026   signal (SIGALRM, catch_alarm);
00027 
00028   /* Set an alarm to go off in a little while. */
00029   alarm (2);
00030 
00031   /* Check the flag once in a while to see when to quit. */
00032   while (keep_going)
00033     do_stuff ();
00034 
00035   return EXIT_SUCCESS;
00036 }