-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarm.c
More file actions
55 lines (48 loc) · 1.12 KB
/
alarm.c
File metadata and controls
55 lines (48 loc) · 1.12 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
An implementation of 'alarm()' using setitimer
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <signal.h>
unsigned int alarm(unsigned int seconds) {
struct itimerval timerval;
timerval.it_interval.tv_sec = 0;
timerval.it_interval.tv_usec = 0;
timerval.it_value.tv_sec = seconds;
timerval.it_value.tv_usec = 0;
return setitimer(ITIMER_REAL, &timerval, NULL);
}
void sighandler(int sig) {
//not async signal safe...
printf("bzzzzzz\n");
}
int main(int argc, char **argv) {
sigset_t block;
struct sigaction sigact;
time_t seconds;
if (argc < 2) {
printf("Usage 'alarm <nseconds>'\n");
exit(EXIT_FAILURE);
}
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigact.sa_handler = sighandler;
if (sigaction(SIGALRM, &sigact, NULL) == -1) {
printf("sigaction\n");
exit(EXIT_FAILURE);
}
seconds = strtol(argv[1], NULL, 10);
sigemptyset(&block);
sigaddset(&block, SIGALRM);
sigprocmask(SIG_BLOCK, &block, NULL);
if (alarm(seconds) == -1) {
printf("alarm\n");
exit(EXIT_FAILURE);
}
sigemptyset(&block);
sigsuspend(&block);
exit(EXIT_SUCCESS);
}