| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <signal.h> |
| 9 |
#include <time.h> |
| 10 |
#include <memory.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <pthread.h> |
| 13 |
|
| 14 |
#define TIMER_SIGNAL (SIGRTMIN+1) |
| 15 |
#define SUCCESS 0 |
| 16 |
#define FAILURE -1 |
| 17 |
|
| 18 |
int |
| 19 |
setup_timer(timer_t *timer) |
| 20 |
{ |
| 21 |
int status; |
| 22 |
struct sigevent sigev; |
| 23 |
|
| 24 |
memset(&sigev, 0, sizeof(sigev)); |
| 25 |
sigev.sigev_notify = SIGEV_SIGNAL; |
| 26 |
sigev.sigev_signo = TIMER_SIGNAL; |
| 27 |
status = timer_create(CLOCK_MONOTONIC, &sigev, timer); |
| 28 |
if (status) { |
| 29 |
fprintf(stderr,"error from timer_create: %s\n", strerror(errno)); |
| 30 |
return FAILURE; |
| 31 |
} |
| 32 |
return SUCCESS; |
| 33 |
} |
| 34 |
|
| 35 |
int |
| 36 |
start_timer(timer_t t, int sec, int nsec) |
| 37 |
{ |
| 38 |
int status; |
| 39 |
struct itimerspec it; |
| 40 |
|
| 41 |
|
| 42 |
memset(&it, 0, sizeof(it)); |
| 43 |
it.it_value.tv_sec = sec; |
| 44 |
it.it_value.tv_nsec = nsec; |
| 45 |
status = timer_settime(t, 0, &it, NULL); |
| 46 |
if (status) |
| 47 |
fprintf(stderr,"starting timer: %s\n", strerror(errno)); |
| 48 |
return status; |
| 49 |
} |
| 50 |
|
| 51 |
int |
| 52 |
wait_for_signal(void) |
| 53 |
{ |
| 54 |
int signo; |
| 55 |
sigset_t sigset; |
| 56 |
|
| 57 |
if (sigemptyset(&sigset)) { |
| 58 |
fprintf(stderr,"creating empty signal wait set: %s\n", strerror(errno)); |
| 59 |
return -1; |
| 60 |
} |
| 61 |
if (sigaddset(&sigset, SIGINT)) { |
| 62 |
fprintf(stderr,"adding SIGINT to signal set: %s\n", strerror(errno)); |
| 63 |
return -1; |
| 64 |
} |
| 65 |
if (sigaddset(&sigset, TIMER_SIGNAL)) { |
| 66 |
fprintf(stderr,"adding TIMER_SIGNAL to signal set: %s\n", strerror(errno)); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
if (sigwait(&sigset, &signo)) { |
| 71 |
fprintf(stderr,"waiting for signal: %s\n", strerror(errno)); |
| 72 |
return -1; |
| 73 |
} |
| 74 |
return signo; |
| 75 |
} |
| 76 |
|
| 77 |
int |
| 78 |
block_signals(void) |
| 79 |
{ |
| 80 |
int status; |
| 81 |
sigset_t sigset; |
| 82 |
|
| 83 |
|
| 84 |
status = sigfillset(&sigset); |
| 85 |
if (status) { |
| 86 |
fprintf(stderr,"setting up full signal set %d\n", status); |
| 87 |
return FAILURE; |
| 88 |
} |
| 89 |
status = pthread_sigmask(SIG_BLOCK, &sigset, NULL); |
| 90 |
if (status) { |
| 91 |
fprintf(stderr,"setting signal mask: %d\n", status); |
| 92 |
return FAILURE; |
| 93 |
} |
| 94 |
return SUCCESS; |
| 95 |
} |
| 96 |
|
| 97 |
int |
| 98 |
main(int argc, char **argv) |
| 99 |
{ |
| 100 |
int status; |
| 101 |
timer_t timer; |
| 102 |
unsigned long count = 0; |
| 103 |
int stop_test = 0; |
| 104 |
|
| 105 |
block_signals(); |
| 106 |
setup_timer(&timer); |
| 107 |
printf("Press Ctrl-C to stop\n"); |
| 108 |
while (stop_test == 0) { |
| 109 |
if (start_timer(timer, 0, 500000000)) { |
| 110 |
stop_test = 1; |
| 111 |
continue; |
| 112 |
} |
| 113 |
status = wait_for_signal(); |
| 114 |
if (status == SIGINT) { |
| 115 |
fputs("\033[1B", stdout); |
| 116 |
stop_test = 1; |
| 117 |
} |
| 118 |
else if (status == TIMER_SIGNAL) { |
| 119 |
printf("count: %lu\n", ++count); |
| 120 |
fputs("\033[1A", stdout); |
| 121 |
} |
| 122 |
else { |
| 123 |
fprintf(stderr, "WTF?\n"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
} |
| 127 |
return 0; |
| 128 |
} |