-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
executable file
·51 lines (37 loc) · 897 Bytes
/
timer.c
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
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
volatile int time_has_come =0;
time_t cur_time;
struct itimerval interval;
void init_timer(void);
void timerHandler(int );
int startTimer(unsigned int);
/****************timer.c **********/
void init_timer( )
{
signal(SIGALRM, timerHandler);
return;
}
void timerHandler(int signal)
{
time_has_come =1;
cur_time = time(NULL);
printf("!!!!!!! %s\n", ctime(&cur_time));
return;
}
int startTimer( unsigned int seconds)
{
interval.it_interval.tv_sec = 0;
interval.it_interval.tv_usec = 0;
interval.it_value.tv_sec = seconds;
interval.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &interval, NULL);
//setitimer(ITIMER_VIRTUAL, &interval, NULL);
cur_time = time(NULL);
printf("start time =%s\n", ctime(&cur_time));
return 0;
}