aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/alarm.c
blob: b8a43e38d02f6aee723443baa4acc867fb38769e (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
#include <signal.h>
#include <assert.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static volatile int alarms_fired = 0;

static void sigalrm_handler(int signal) {
	if(signal == SIGALRM)
		alarms_fired++;
}

int main() {
	signal(SIGALRM, sigalrm_handler);

	alarms_fired = 0;

	unsigned int ret = alarm(10);
	assert(!ret);

	sleep(1);

	ret = alarm(1);
	assert(ret == 9);

	sleep(2);

	if(alarms_fired != 1) {
		fprintf(stderr, "alarm handler fired %u times instead of 1\n", alarms_fired);
		exit(1);
	}
}