sigh1.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Signal Handlers that Return
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <signal.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. /* This flag controls termination of the main loop. */
  18. volatile sig_atomic_t keep_going = 1;
  19. /* The signal handler just clears the flag and re-enables itself. */
  20. void
  21. catch_alarm (int sig)
  22. {
  23. keep_going = 0;
  24. signal (sig, catch_alarm);
  25. }
  26. void
  27. do_stuff (void)
  28. {
  29. puts ("Doing stuff while waiting for alarm....");
  30. }
  31. int
  32. main (void)
  33. {
  34. /* Establish a handler for SIGALRM signals. */
  35. signal (SIGALRM, catch_alarm);
  36. /* Set an alarm to go off in a little while. */
  37. alarm (2);
  38. /* Check the flag once in a while to see when to quit. */
  39. while (keep_going)
  40. do_stuff ();
  41. return EXIT_SUCCESS;
  42. }