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的更多相关文章
随机推荐
- Oracle高级查询之over(partition by...)
现有表,数据如下: eg1:查询年龄第二的队员 通常写法: select * from (select a.*, rownum r from (select t.* from l_student_in ...
- 解决ajax跨域
今天要联调项目,前后端请求使用ajax,联调存在跨域问题,解决办法如下: (1)在本地的电脑上新建一个文件夹,用于前后端联调存放浏览器 缓存的 (2)打开桌面的谷歌浏览器图标(右键>属性> ...
- myEclipse和eclipse从debug视图自动跳回default视图。
本来是吐槽文,找到了解决的插件,就改改标题了. debug的时候,可以从default视图自动跳转到debug视图,退出debug的时候,却不能自动切换回default视图. https://bugs ...
- 将对象转成 json 以及 将字符串 hash(SHA1) 加密
如下: /// <summary> /// 生成 Json /// </summary> /// <param name="obj"></ ...
- ckeditor5字体颜色,字体背景颜色设置显示
在config.js中添加相关代码: config.allowedContent=true;//关闭标签过滤, config.colorButton_enableAutomatic = true; c ...
- 2015-112 ado.net2
CRUD:create read update delete 七. 数据绑定数据列的转换 在gridview中添加<OnRowDataBound="GridView1_RowDataB ...
- python 变量之小整数池跟大整数池
在python中定义变量会有:id,type,value.对于==比较的是value,对于is比较的是id. 因此,对于相同value的变量,它的type相同,但是它的id值可能不一样.对于相同id的 ...
- mysql innodb 唯一键里的字段为什么不能为NULL
mysql 唯一键失效 CREATE TABLE `studnet_unique` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100 ...
- learning makefile ?=
- java根据#号截取字符串,使用Pattern的方法
public class Regex1 { public static void main(String[] args) { String s = "神秘的海洋出现了一只#话题#海怪阿拉斯加 ...