1. #ifndef __ALRM_H
  2. #define __ALRM_H
  3.  
  4. #define MAX 1024
  5.  
  6. typedef void (*any_t)(void *s);
  7.  
  8. typedef struct {
  9. int times;
  10. any_t any;
  11. void *p;
  12. }alarm_t;
  13.  
  14. //初始化
  15. int alrm_init(int t, any_t a, void *ptr);
  16.  
  17. //销毁
  18. void akrm_destroy(int i);
  19.  
  20. #endif
  21.  
  1. #include <stdlib.h>
  2. #include <sys/time.h>
  3. #include <signal.h>
  4. #include <errno.h>
  5. #include <unistd.h>
  6.  
  7. #include "alrm.h"
  8.  
  9. static alarm_t *arr[MAX];
  10.  
  11. static int inited;
  12. static struct sigaction oldact;
  13. static struct itimerval olditv;
  14.  
  15. void alrm_destroy(int i);
  16.  
  17. static int get_pos(void)
  18. {
  19. for(int i; i<MAX; i++){
  20. if(NULL == arr[i])
  21. return i;
  22. }
  23. return -;
  24. }
  25.  
  26. static void alrm_handler(int s)
  27. {
  28. for(int i = ; i< MAX; i++){
  29. if(NULL != arr[i]){
  30. if(arr[i]->times > )
  31. arr[i]->times -= ;
  32. }else {
  33. arr[i]->any(arr[i]->p);
  34. alrm_destroy(i);
  35. }
  36. }
  37. }
  38. //信号行为 时钟恢复
  39. static void moduler_unload(void)
  40. {
  41. sigaction(SIGALRM, &oldact, NULL);
  42. setitimer(ITIMER_REAL, &olditv, NULL);
  43. }
  44.  
  45. static void moduler_load(void)
  46. {
  47. struct sigaction act;
  48. struct itimerval itv;
  49.  
  50. act.sa_handler = alrm_handler;
  51. act.sa_flags = ;
  52. sigemptyset(&(act.sa_mask));
  53. sigaction(SIGALRM, &act, &oldact);
  54.  
  55. itv.it_interval.tv_sec = ;
  56. itv.it_interval.tv_usec = ;
  57.  
  58. itv.it_value.tv_sec = ;
  59. itv.it_value.tv_usec = ;
  60. setitimer(ITIMER_REAL, &itv, &olditv);
  61.  
  62. atexit(moduler_unload);
  63. }
  64.  
  65. //初始化
  66. int alrm_init(int t, any_t a, void *ptr)
  67. {
  68. alarm_t *alm = NULL;
  69. int pos;
  70.  
  71. if(inited == ){
  72. moduler_load();
  73. inited = ;
  74. }
  75. alm = malloc(sizeof(*alm));
  76. if(NULL == alm)
  77. return -ENOMEM;
  78. alm->times = t;
  79. alm->any = a;
  80. alm->p = ptr;
  81.  
  82. pos = get_pos();
  83. if(pos < ){
  84. free(alm);
  85. return -ENOSPC;
  86. }
  87. arr[pos] = alm;
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. #include "alrm.h"
  5. static void any1(void *s)
  6. {
  7. printf("%s", (char *)s);
  8. fflush(NULL);
  9. }
  10.  
  11. static void any2(void *s)
  12. {
  13. printf("%s", (char *)s);
  14. fflush(NULL);
  15. }
  16.  
  17. static void any3(void *s)
  18. {
  19. printf("%s", (char *)s);
  20. fflush(NULL);
  21. }
  22.  
  23. int main(void)
  24. {
  25. int val1, val2, val3;
  26.  
  27. val1 = alrm_init(, any1, "hello");
  28. val2 = alrm_init(, any2, "world");
  29. val3 = alrm_init(, any3, "apue");
  30.  
  31. /*
  32. **world*hello**apue******
  33. */
  34. while () {
  35. write(, "*", );
  36. sleep();
  37. }
  38.  
  39. return ;
  40. }
  1.  
  1. return pos;
  2. }
  3.  
  4. //销毁
  5. void alrm_destroy(int i){
  6. free(arr[i]);
  7. arr[i] = NULL;
  8. }
  1.  
  1.  

anyalarm的更多相关文章

随机推荐

  1. HOMER | MEME | 转录因子的靶基因预测

    Finding Enriched Motifs in Genomic Regions (findMotifsGenome.pl) 在指定区域做motif enrichment,大大降低了假阳性. ME ...

  2. 重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    出现报错: Warning: World-writable config file '/etc/my.cnf' is ignored // 该文件权限过高ERROR 1045 (28000): Acc ...

  3. 【C#】【对象转XML】xml序列化

    笔记:xml序列化 /// <summary>        /// xml序列化        /// </summary>        /// <param nam ...

  4. [cf contest246] E - Blood Cousins Return

    [cf contest246] E - Blood Cousins Return time limit per test 3 seconds memory limit per test 256 meg ...

  5. C#用正则表达式替换手机中间几位为*号 代码及解析

    /// <summary> /// 替换手机号中间四位为* /// </summary> /// <param name="phoneNo">& ...

  6. jquery <div> 排序

    var asc_active = function(a, b) { var av = Number($(a).find('.active_num .v').html()); var bv = Numb ...

  7. Python3+unittest使用教程

    一.直接使用TestCase 注意所有测试方法都需要以test开头.代码如下: import unittest class Test1(unittest.TestCase): @classmethod ...

  8. FastDFS 与 Nginx 实现分布式图片服务器

    FastDFS 与 Nginx 实现分布式图片服务器 本人的 Ubuntu18.04 用户名为 jj 点我下载所有所需的压缩包文件 一.FastDFS安装 1.安装 fastdfs 依赖包 ① 解压 ...

  9. echarts x和y去掉

    解决方法 "axisLine": { "show": false },

  10. AngelToken揭秘区块链之四大链

    区块链,有着各种不同,与之相对应的就是内涵和功能.在区块链领域经常出现的四大链有:公有链.私有链.联盟链.许可链,这些链又分别可以为区块链干什么呢? 公有链(Public Blockchain) 是指 ...