summaryrefslogtreecommitdiff
path: root/lab09/timer.c
blob: 467b3b4b2a9a1fcd3cdf2de1b9ce2fa4da05a62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>

#define RATE	5	/* 5 Hz */

int count = 0;

void alarm_handler(int x) {
	count++;
	if ((count > 0) && ((count % 10) == 0)) {
		printf("Number of timer callbacks: %d\n", count);
	}
	signal(SIGALRM, alarm_handler);
}

int main()
{
	struct timeval initial  = {0, 1000000/RATE};
	struct timeval interval = {0, 1000000/RATE};
	struct itimerval t;

	t.it_value = initial;
	t.it_interval = interval;
	if (signal(SIGALRM, alarm_handler) != SIG_ERR) {
		if (!setitimer(ITIMER_REAL, &t, 0)) {
			/* Perform other tasks waiting for our SIGALRMs */
			while(count <= 50) {
				;
			}
		}
	}
	exit(0);
}