myalarm
Posted boxiansheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了myalarm相关的知识,希望对你有一定的参考价值。
main.c
1 #include<stdio.h> 2 #include<unistd.h> 3 #include"myalarm.h" 4 5 int main(void) 6 { 7 anytimer_alarm(3,any1,"hello"); 8 anytimer_alarm(2,any2,"world"); 9 anytimer_alarm(5,any3,"apue"); 10 11 while(1) 12 { 13 write(1,"*",1); 14 sleep(1); 15 } 16 return 0; 17 18 }
myalarm.c
1 #include<stdio.h> 2 #include<signal.h> 3 #include<stdlib.h> 4 #include<sys/time.h> 5 #include<unistd.h> 6 #include<errno.h> 7 #include"myalarm.h" 8 9 static int inited; 10 typedef struct anyalarm 11 { 12 int sec; 13 void (*fcntl)(void *);//函数指针(它是一个指针,它指向一个函数的地址),用来接受字符串,并打印出来, 14 char *str;//接受字符串的首地址 15 }ANYALRM; 16 static ANYALRM *alr[ALRSIZE]; 17 static struct sigaction oldact; 18 static struct itimerval olditv; 19 20 static void alrm_handler(int s) 21 { 22 // printf("111\n"); 23 for(int i=0;i<ALRSIZE;i++) 24 { 25 if(alr[i] != NULL) 26 { 27 alr[i]->sec -= 1; 28 if(alr[i]->sec==0)//当秒为0的时候则输出字符串 29 { 30 alr[i]->fcntl(alr[i]->str);//通过函数指针打印出来字符串, 31 free(alr[i]); 32 alr[i]=NULL;//清空数组中的地址 33 } 34 } 35 36 } 37 } 38 void moduler_load(void) 39 { 40 struct sigaction act;//改变信号行为 41 struct itimerval itv; 42 43 act.sa_handler = alrm_handler; 44 act.sa_flags = 0; 45 sigemptyset(&act.sa_mask); 46 sigaction(SIGALRM,&act,&oldact); 47 48 itv.it_interval.tv_sec = 1; 49 itv.it_interval.tv_usec = 0; 50 itv.it_value.tv_sec = 1; 51 itv.it_value.tv_usec = 0; 52 setitimer(ITIMER_REAL,&itv,&olditv); 53 54 atexit(moduler_unload);//取消挂载,使闹钟恢复默认状态 55 56 } 57 void moduler_unload(void)//信号行为和时钟的恢复 58 { 59 sigaction(SIGALRM,&oldact,NULL); 60 setitimer(ITIMER_REAL,&olditv,NULL); 61 } 62 63 void any1(void *s) 64 { 65 printf("%s",(char *)s); 66 fflush(NULL); 67 } 68 void any2(void *s) 69 { 70 printf("%s",(char *)s); 71 fflush(NULL); 72 } 73 void any3(void *s) 74 { 75 printf("%s",(char *)s); 76 fflush(NULL); 77 } 78 79 80 int alr_init(int sec,void (*fcntl)(void *),char *str) 81 { 82 ANYALRM *alrm = NULL; 83 if(inited ==0) 84 { 85 moduler_load(); 86 inited = 1; 87 } 88 alrm=malloc(sizeof(ANYALRM)); 89 if(alrm != NULL) 90 { 91 // fprintf(stderr,"malloc is failed!\n"); 92 alrm->sec = sec; 93 alrm->fcntl = fcntl; 94 alrm->str = str; 95 for(int i=0;i<ALRSIZE;i++) 96 { 97 if(alr[i]==NULL) 98 { 99 alr[i]=alrm; 100 return i; 101 } 102 } 103 return -1; 104 } 105 } 106 void anytimer_alarm(int sec,void(*fcntl)(void *),char *str) 107 { 108 if(alr_init(sec,fcntl,str)<0) 109 { 110 fprintf(stderr,"*alr_init failer\n"); 111 return ; 112 } 113 }
myalarm.h
1 #ifndef __MYALARM_H 2 #define __MYALARM_H 3 4 #define ALRSIZE 1024 5 6 void any1(void *s); 7 void any2(void *s); 8 void any3(void *s); 9 10 void anytimer_alarm(int sec,void(*fcntl)(void *),char *str); 11 12 int alr_init(int sec,void (*fcntl)(void *),char *str); 13 14 void alarm_handler(int s); 15 16 int alr_init(int sec,void(*fcntl)(void *),char *str); 17 18 void moduler_load(void); 19 20 void moduler_unload(void); 21 22 23 #endif
makefile
1 main : main.o myalarm.o 2 gcc main.o myalarm.o -o main 3 clean : 4 rm -rf *.o main
以上是关于myalarm的主要内容,如果未能解决你的问题,请参考以下文章