Blame manual/examples/sigh1.c

Packit 6c4009
/* Signal Handlers that Return
Packit 6c4009
   Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU General Public License
Packit 6c4009
   as published by the Free Software Foundation; either version 2
Packit 6c4009
   of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
Packit 6c4009
*/
Packit 6c4009
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
/* This flag controls termination of the main loop. */
Packit 6c4009
volatile sig_atomic_t keep_going = 1;
Packit 6c4009
Packit 6c4009
/* The signal handler just clears the flag and re-enables itself. */
Packit 6c4009
void
Packit 6c4009
catch_alarm (int sig)
Packit 6c4009
{
Packit 6c4009
  keep_going = 0;
Packit 6c4009
  signal (sig, catch_alarm);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
do_stuff (void)
Packit 6c4009
{
Packit 6c4009
  puts ("Doing stuff while waiting for alarm....");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (void)
Packit 6c4009
{
Packit 6c4009
  /* Establish a handler for SIGALRM signals. */
Packit 6c4009
  signal (SIGALRM, catch_alarm);
Packit 6c4009
Packit 6c4009
  /* Set an alarm to go off in a little while. */
Packit 6c4009
  alarm (2);
Packit 6c4009
Packit 6c4009
  /* Check the flag once in a while to see when to quit. */
Packit 6c4009
  while (keep_going)
Packit 6c4009
    do_stuff ();
Packit 6c4009
Packit 6c4009
  return EXIT_SUCCESS;
Packit 6c4009
}