main.c

 1 #include<stdio.h>
 2 #include<unistd.h>
 3 #include<sys/time.h>
 4 
 5 #include"myalarm.h"
 6 
 7 int main(void)
 8 {
 9     anytimer_alarm(3,any1,"hello");
10     anytimer_alarm(2,any2,"world");
11     anytimer_alarm(5,any3,"apue");
12 
13     while(1)
14     {
15         write(1,"*",1);
16         sleep(1);
17     }
18     return 0;
19 }

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 
  8 #include"myalarm.h"
  9 
 10 int inited=0;
 11 
 12 typedef struct anyalarm
 13 {
 14     int sec;
 15     void (*fcntl)(void *);
 16     char *str;
 17 }ANYALRM;
 18 
 19 ANYALRM *alr[ALRSIZE];
 20 static struct sigaction oldact;
 21 static struct itimerval olditv;
 22 
 23 static void alrm_handler(int s)
 24 {
 25     for(int i=0;alr[i]!=NULL;i++)
 26     {
 27         alr[i]->sec-=1;
 28         if(alr[i]->sec==0)
 29         {
 30             alr[i]->fcntl(alr[i]->str);
 31             free(alr[i]);
 32         }
 33     }
 34 }
 35 
 36 void moduler_load()
 37 {
 38     struct sigaction act;
 39     struct itimerval itv;
 40 
 41     act.sa_handler=alrm_handler;
 42     act.sa_flags=0;
 43     sigemptyset(&act.sa_mask);
 44     sigaction(SIGALRM,&act,&oldact);
 45     itv.it_interval.tv_sec=1;
 46     itv.it_interval.tv_usec=0;
 47     itv.it_value.tv_sec=1;
 48     itv.it_value.tv_usec=0;
 49     setitimer(ITIMER_REAL,&itv,&olditv);
 50     atexit(moduler_unload);
 51 }
 52 
 53 //信号行为和时钟得到恢复
 54 void moduler_unload()
 55 {
 56     sigaction(SIGALRM,&oldact,NULL);
 57     setitimer(ITIMER_REAL,&olditv,NULL);
 58 }
 59 
 60 void any1(void *s)
 61 {
 62     printf("%s\n",(char *)s);    
 63 }
 64 
 65 void any2(void *s)
 66 {
 67     printf("%s\n",(char *)s);    
 68 }
 69 
 70 void any3(void *s)
 71 {
 72     printf("%s\n",(char *)s);    
 73 }
 74 
 75 int alr_init(int sec,void(*fcntl)(void *),char *str)
 76 {
 77     ANYALRM *alrm=NULL;
 78     //第一次调用
 79     if(inited==0)
 80     {
 81         moduler_load();
 82         inited=1;
 83     }
 84     alrm=malloc(sizeof(ANYALRM));
 85     if(alrm==NULL)
 86     {
 87         fprintf(stderr,"malloc is failed!\n");    
 88     }
 89     alrm->sec=sec;
 90     alrm->fcntl=fcntl;
 91     alrm->str=str;
 92     for(int i=0;i<ALRSIZE;i++)
 93     {
 94         if(alr[i]==NULL)
 95         {
 96             alr[i]=alrm;
 97             return 1;
 98         }
 99     }
100     return -1; 
101 }
102 
103 void anytimer_alarm(int sec,void (*fcntl)(void *),char *str)
104 {
105     if(alr_init(sec,fcntl,str)<0)
106     {
107         fprintf(stderr,"alr_init failed\n");
108         return;
109     }
110 }

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 #endif

makefile

1 main :main.o myalarm.o
2     gcc main.o myalarm.o -o main
3 clean :
4     rm -rf *.o main