-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
80 lines (66 loc) · 1.57 KB
/
main.c
File metadata and controls
80 lines (66 loc) · 1.57 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "timer.h"
void say_hello(void *arg);
void print_value(void *arg);
void print_rectangle(void *arg);
void sigintr_handler(int signo);
int main(void)
{
/* 初始化闹钟 */
if (timer_init() != 0) {
fprintf(stderr, "init timer failed.\n");
exit(1);
}
/* 安装中断信号处理函数 */
if (signal(SIGINT, sigintr_handler) == SIG_ERR) {
fprintf(stderr, "set SIGINT handler failed.\n");
exit(2);
}
/* 添加3个任务 */
TimerId_t ids[3];
ids[0] = timer_add(2,say_hello,NULL,LOOP);
if(ids[0] == ADD_FAILED) {
fprintf(stderr, "add timer failed.\n");
timer_destroy();
return 2;
}
int value = 555;
ids[1] = timer_add(5, print_value, &value, ONCE);
ids[2] = timer_add(3, print_rectangle, NULL, LOOP);
/* 删除第0个任务 */
getchar();
printf("\033[31mDel A Timer.\n\033[0m");
timer_del(ids[0]);
/* 销毁所有任务 */
printf("Enter Destory Timer.\n");
getchar();
timer_destroy();
return 0;
}
void sigintr_handler(int signo)
{
if (signo == SIGINT) {
printf("\n\033[0m");
exit(22);
}
}
void say_hello(void *arg)
{
printf("\033[36mHello,Hello...\033[0m\n");
}
void print_value(void *arg)
{
printf("\033[31m=====value:%d======\n\033[0m", *(int *)arg);
}
void print_rectangle(void *arg)
{
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
printf("\033[32m* \033[0m");
}
putchar('\n');
}
}