时钟控制字符串输出

  1 /*
  2     时钟控制字符串输出
  3     **world*hello**apue******
  4 */
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <signal.h>
  8 #include <string.h>
  9 #include <sys/time.h>
 10 #include <unistd.h>
 11 #include <errno.h>
 12 
 13 #define MAXALARM 1024
 14 
 15 typedef struct {  //显示字符串
 16     int sec;
 17     void (*handler)(void *s);
 18     char s[20];
 19 }ar;
 20 
 21 static ar *arr[MAXALARM];
 22 static int inited;
 23 
 24 static int get_pos(void)
 25 {
 26     for(int i = 0; i < MAXALARM; i++)
 27     {
 28         if(arr[i] == NULL)
 29         {
 30             return i;
 31         }
 32     }
 33 }
 34 
 35 static void print(int i)//打印数组成员
 36 {    
 37     (arr[i]->handler)(arr[i]->s);
 38     arr[i] = NULL;
 39     free(arr[i]);
 40 }
 41 
 42 static void handleri(int s)
 43 {
 44     for(int i = 0; i < MAXALARM; i++)
 45     {
 46         if(arr[i] != NULL)
 47         {
 48             arr[i]->sec -= 1;
 49             if(arr[i]->sec == 0)
 50         print(i);
 51         }
 52     } 
 53 }
 54 
 55 static void statdo(void)//定义时钟
 56 {
 57     struct sigaction act, oldact;
 58     struct itimerval itv, olditv;
 59 
 60     act.sa_handler = handleri;
 61     act.sa_flags = 0;
 62     sigemptyset(&act.sa_mask);
 63     sigaction(SIGALRM, &act, &oldact);
 64 
 65     itv.it_interval.tv_sec = 1;
 66     itv.it_interval.tv_usec = 0;
 67     itv.it_value.tv_sec = 1;
 68     itv.it_value.tv_usec = 0;
 69     setitimer(ITIMER_REAL, &itv, &olditv);
 70 
 71 }
 72 
 73 void anytimer_alarm(int sec, void (*handler)(void *s), char *p)
 74 {
 75     ar *t = NULL;
 76     int pos;
 77 
 78     
 79     if(!inited)
 80     {
 81         statdo();
 82         inited = 1;
 83     }
 84     
 85     t = malloc(sizeof(ar));
 86     if(NULL == t)
 87         return ;
 88 
 89     t->sec = sec;
 90     t->handler = handler;
 91     strcpy(t->s,p);
 92 
 93     pos = get_pos();
 94     if(pos < 0)
 95     {
 96         free(t);
 97         return ;
 98     }
 99 
100     arr[pos] = t;
101 }
102 
103 static void any1(void *s)
104 {
105     printf("%s", (char *)s);
106     fflush(NULL);
107 }
108 
109 static void any2(void *s)
110 {
111     printf("%s", (char *)s);
112     fflush(NULL);
113 }
114 
115 static void any3(void *s)
116 {
117     printf("%s", (char *)s);
118     fflush(NULL);
119 }
120 
121 int main(void)
122 {
123     anytimer_alarm(3, any1, "hello");
124     anytimer_alarm(2, any2, "world");
125     anytimer_alarm(5, any3, "apue");
126 
127     /*
128      **world*hello**apue******
129      */
130     while (1) {
131         write(1, "*", 1);
132         sleep(1);
133     }
134 
135     return 0;
136 }

 

转载于:https://www.cnblogs.com/wyy-lxt/p/10590810.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值