Line | |
---|
1 | #include <signal.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | |
---|
5 | /* This flag controls termination of the main loop. */ |
---|
6 | volatile sig_atomic_t keep_going = 1; |
---|
7 | |
---|
8 | /* The signal handler just clears the flag and re-enables itself. */ |
---|
9 | void |
---|
10 | catch_alarm (int sig) |
---|
11 | { |
---|
12 | keep_going = 0; |
---|
13 | puts("ALARM"); |
---|
14 | signal (sig, catch_alarm); |
---|
15 | } |
---|
16 | |
---|
17 | void |
---|
18 | do_stuff (void) |
---|
19 | { |
---|
20 | puts ("Doing stuff while waiting for alarm...."); |
---|
21 | } |
---|
22 | |
---|
23 | int |
---|
24 | main (void) |
---|
25 | { |
---|
26 | /* Establish a handler for SIGALRM signals. */ |
---|
27 | signal (SIGALRM, catch_alarm); |
---|
28 | |
---|
29 | /* Set an alarm to go off in a little while. */ |
---|
30 | alarm (2); |
---|
31 | |
---|
32 | /* Check the flag once in a while to see when to quit. */ |
---|
33 | while (keep_going) |
---|
34 | do_stuff (); |
---|
35 | |
---|
36 | return EXIT_SUCCESS; |
---|
37 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.