闹钟

alarm.c

  1 #include<stdio.h>
  2 #include<signal.h>
  3 #include<sys/time.h>
  4 #include<stdlib.h>
  5 #include"alarm.h"
  6 
  7 static struct sigaction oldact;
  8 static struct itimerval olditv;
  9 
 10 static void moduler_unload(void);
 11 typedef struct
 12 {
 13     int token;
 14     p q;
 15     char *c;
 16 }TBF;
 17 TBF *tbf[BUFFSIZE]={};
 18 static int pos=0;
 19  void any1(void *s)
 20 {
 21     printf("%s",(char *)s);
 22     fflush(NULL);
 23 }
 24  void any2(void *s)
 25 {
 26     printf("%s",(char *)s);
 27     fflush(NULL);
 28 }
 29  void any3(void *s)
 30 {
 31     printf("%s",(char *)s);
 32     fflush(NULL);
 33 }
 34  void alrm_handler(int s)
 35 {
 36     for(int i=0;i<BUFFSIZE;i++){
 37         if(tbf[i]!=NULL){
 38             tbf[i]->token-=1;
 39             if(tbf[i]->token == 0){
 40                 (tbf[i]->q)(tbf[i]->c);
 41         
 42                 free(tbf[i]);
 43                 tbf[i]==NULL;
 44             }
 47         }
 48     }
 49 }
 50 static void moduler_load(void)
 51 {
 52     struct sigaction act;
 53     struct itimerval itv;
 54 
 55     act.sa_handler=alrm_handler;
 56     act.sa_flags=0;
 57     sigemptyset(&act.sa_mask);
 58     sigaction(SIGALRM,&act,&oldact);
 59 
 60     itv.it_interval.tv_sec=1;
 61     itv.it_interval.tv_usec=0;
 62     itv.it_value.tv_sec=1;
 63     itv.it_value.tv_usec=0;
 64     setitimer(ITIMER_REAL,&itv,&olditv);
 65 
 66     atexit(moduler_unload);
 67 }
 68 static void moduler_unload(void)
 69 {
 70     sigaction(SIGALRM,&oldact,NULL);
 71     setitimer(ITIMER_REAL,&olditv,NULL);
 72 }
 73 void anytimer_alarm(int s,p any,char *ss)
 74 {
 75     TBF *tk=NULL;
 76     if(pos==0){
 77         moduler_load();
 78         pos=1;
 79     }
 80     tk=malloc(sizeof(TBF));
 81     if(tk==NULL){
 82         printf("malloc failed");
 83         return;
 84     }
 85     tk->token=s;
 86     tk->q=any;
 87     tk->c=ss;
 88     
 89     tbf[pos]=tk;
 90     pos++;
 91 }
 92 

alarm.h

  1 #ifndef __ALARM_H                                                                                                                                                                                                                                                                                                             
  2 #define __ALARM_H
  3 
  4 #define BUFFSIZE 10
  5 
  6 typedef void (*p)(void *);
  7  void any1(void *s);
  8  void any2(void *s);
  9  void any3(void *s);
 10 
 11 void anytimer_alarm(int s,p any,char *ss);
 12 
 13 
 14 
 15 
 16 #endif

main.c

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

makefile

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

结果:

**world*hello**apue***^C

猜你喜欢

转载自www.cnblogs.com/lmhya/p/10589604.html