anyalarm
- #ifndef __ALRM_H
- #define __ALRM_H
- #define MAX 1024
- typedef void (*any_t)(void *s);
- typedef struct {
- int times;
- any_t any;
- void *p;
- }alarm_t;
- //初始化
- int alrm_init(int t, any_t a, void *ptr);
- //销毁
- void akrm_destroy(int i);
- #endif
- #include <stdlib.h>
- #include <sys/time.h>
- #include <signal.h>
- #include <errno.h>
- #include <unistd.h>
- #include "alrm.h"
- static alarm_t *arr[MAX];
- static int inited;
- static struct sigaction oldact;
- static struct itimerval olditv;
- void alrm_destroy(int i);
- static int get_pos(void)
- {
- for(int i; i<MAX; i++){
- if(NULL == arr[i])
- return i;
- }
- return -;
- }
- static void alrm_handler(int s)
- {
- for(int i = ; i< MAX; i++){
- if(NULL != arr[i]){
- if(arr[i]->times > )
- arr[i]->times -= ;
- }else {
- arr[i]->any(arr[i]->p);
- alrm_destroy(i);
- }
- }
- }
- //信号行为 时钟恢复
- static void moduler_unload(void)
- {
- sigaction(SIGALRM, &oldact, NULL);
- setitimer(ITIMER_REAL, &olditv, NULL);
- }
- static void moduler_load(void)
- {
- struct sigaction act;
- struct itimerval itv;
- act.sa_handler = alrm_handler;
- act.sa_flags = ;
- sigemptyset(&(act.sa_mask));
- sigaction(SIGALRM, &act, &oldact);
- itv.it_interval.tv_sec = ;
- itv.it_interval.tv_usec = ;
- itv.it_value.tv_sec = ;
- itv.it_value.tv_usec = ;
- setitimer(ITIMER_REAL, &itv, &olditv);
- atexit(moduler_unload);
- }
- //初始化
- int alrm_init(int t, any_t a, void *ptr)
- {
- alarm_t *alm = NULL;
- int pos;
- if(inited == ){
- moduler_load();
- inited = ;
- }
- alm = malloc(sizeof(*alm));
- if(NULL == alm)
- return -ENOMEM;
- alm->times = t;
- alm->any = a;
- alm->p = ptr;
- pos = get_pos();
- if(pos < ){
- free(alm);
- return -ENOSPC;
- }
- arr[pos] = alm;
- #include <stdio.h>
- #include <unistd.h>
- #include "alrm.h"
- static void any1(void *s)
- {
- printf("%s", (char *)s);
- fflush(NULL);
- }
- static void any2(void *s)
- {
- printf("%s", (char *)s);
- fflush(NULL);
- }
- static void any3(void *s)
- {
- printf("%s", (char *)s);
- fflush(NULL);
- }
- int main(void)
- {
- int val1, val2, val3;
- val1 = alrm_init(, any1, "hello");
- val2 = alrm_init(, any2, "world");
- val3 = alrm_init(, any3, "apue");
- /*
- **world*hello**apue******
- */
- while () {
- write(, "*", );
- sleep();
- }
- return ;
- }
- return pos;
- }
- //销毁
- void alrm_destroy(int i){
- free(arr[i]);
- arr[i] = NULL;
- }
anyalarm的更多相关文章
随机推荐
- HOMER | MEME | 转录因子的靶基因预测
Finding Enriched Motifs in Genomic Regions (findMotifsGenome.pl) 在指定区域做motif enrichment,大大降低了假阳性. ME ...
- 重置密码解决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 ...
- 【C#】【对象转XML】xml序列化
笔记:xml序列化 /// <summary> /// xml序列化 /// </summary> /// <param nam ...
- [cf contest246] E - Blood Cousins Return
[cf contest246] E - Blood Cousins Return time limit per test 3 seconds memory limit per test 256 meg ...
- C#用正则表达式替换手机中间几位为*号 代码及解析
/// <summary> /// 替换手机号中间四位为* /// </summary> /// <param name="phoneNo">& ...
- jquery <div> 排序
var asc_active = function(a, b) { var av = Number($(a).find('.active_num .v').html()); var bv = Numb ...
- Python3+unittest使用教程
一.直接使用TestCase 注意所有测试方法都需要以test开头.代码如下: import unittest class Test1(unittest.TestCase): @classmethod ...
- FastDFS 与 Nginx 实现分布式图片服务器
FastDFS 与 Nginx 实现分布式图片服务器 本人的 Ubuntu18.04 用户名为 jj 点我下载所有所需的压缩包文件 一.FastDFS安装 1.安装 fastdfs 依赖包 ① 解压 ...
- echarts x和y去掉
解决方法 "axisLine": { "show": false },
- AngelToken揭秘区块链之四大链
区块链,有着各种不同,与之相对应的就是内涵和功能.在区块链领域经常出现的四大链有:公有链.私有链.联盟链.许可链,这些链又分别可以为区块链干什么呢? 公有链(Public Blockchain) 是指 ...