linux C 基于链表链的定时器

源码如下:
util_timer.h

#ifndef LST_TIMER
#define LST_TIMER


#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>




#define BUFFER_SIZE 64
struct util_timer;


/*
struct client_data{
sockaddr_in address;
int sockfd;
char buf[BUFFER_SIZE];
util_timer * timer;
};
*/


struct util_timer{
time_t expire;
// client_data * user_data;
char *buffer;
struct util_timer *prev;
struct util_timer *next;
};


struct timer_lst{
struct util_timer * head;
struct util_timer * tail;
};




void cb_func(unsigned char *userdata);
void add_timer2(struct util_timer * timer, struct util_timer *lst_head);
void add_timer(struct util_timer *timer);
void adjust_timer(struct util_timer *timer);
void del_timer(struct util_timer * timer);
void tick();


#endif

util_timer.c
#include "util_timer.h"


struct util_timer * head;
struct util_timer * tail;




void print_hex_ex(unsigned char *buf, int len)
{
    int i, n;
    printf("            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");
    n = 0;
    for (i=0; i<len; i++)
    {
        if (i % 0x10 == 0) {
            printf("\n%08Xh: ", i);
        }
        printf("%02X ", buf[i]);
    }
printf("\nlens:%d\n\n",len);
}


void cb_func(unsigned char *userdata){
print_hex_ex(userdata, 8);
return;
}


void add_timer2(struct util_timer * timer, struct util_timer * lst_head){
struct util_timer * prev = lst_head;
struct util_timer * tmp = prev->next;
while(tmp){
if(timer->expire < tmp->expire ){
prev->next = timer;
timer->next =tmp;
tmp->prev = timer;
timer->prev = prev;
break;
}
prev = tmp;
tmp = tmp->next;
}
if(!tmp){
prev->next = timer;
timer->prev = prev;
timer->next = NULL;
tail = timer;
}

}


void add_timer(struct util_timer *timer){
if(!timer){
return;
}

if(!head){
head = tail = timer;
return ;
}

if(timer->expire < head->expire){
timer->next = head;
head->prev = timer;
head = timer;

return;
}

add_timer2(timer, head);

}


void adjust_timer(struct util_timer *timer){
if(!timer){
return;
}
struct util_timer * tmp = timer->next;
if(!tmp || (timer->expire < tmp->expire)){
return;
}
if(timer == head){
head = head->next;
head->prev = NULL;
timer->next = NULL;
add_timer2(timer, head);
}else{
timer->prev->next = timer->next;
timer->next->prev = timer->prev;
add_timer2(timer, timer->next);
}

}


void del_timer(struct util_timer * timer){
if(!timer){
return;
}
if((timer == head) && (timer == tail)){
free(timer);
head = NULL;
tail = NULL;
return;
}
if(timer == head){
head = head->next;
head->prev = NULL;
free(timer);
return;
}
if(timer == tail){
tail = tail ->prev;
tail->next = NULL;
free(timer);
return;
}

timer->prev->next = timer->next;
    timer->next->prev = timer->prev;
free(timer);

}


void tick(){
if( !head )
     {
        return;
     }
     printf( "timer tick\n" );
     time_t cur = time( NULL );
     struct util_timer* tmp = head;
     while( tmp )
     {
         if( cur < tmp->expire )
         {
printf("tmp->expire is not chaoshi\n");
             break;
         }
cb_func( tmp->buffer );
         head = tmp->next;
         if( head )
         {
             head->prev = NULL;
         }
         free(tmp);
         tmp = head;
     }

}






int main(){
int i;
char *tmpbuf[5]={"aaaaaaaa","bbbbbbbb","cccccccc","dddddddd","eeeeeeee"};
head = NULL;
tail = NULL;
time_t cur = time(NULL);
for(i = 0; i<5; i++){

struct util_timer *timer = NULL ;
timer = (struct util_timer *)malloc(sizeof(struct util_timer)*1);
cur = cur + 1;
timer->expire = cur;
timer->buffer = tmpbuf[i];
add_timer(timer);
}

printf("sleep begin \n");
sleep(3);
printf("sleep end \n");

tick();
}

编译gcc -o util_timer util_timer.c
运行./util_timer
输出:
[root@hsm timer_test]# ./util_timer
sleep begin 
sleep end 
timer tick
            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
00000000h: 61 61 61 61 61 61 61 61 
lens:8


            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
00000000h: 62 62 62 62 62 62 62 62 
lens:8


            0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
00000000h: 63 63 63 63 63 63 63 63 
lens:8


tmp->expire is not chaoshi

总结:链表共五个节点,每个节点相差1秒,按时间大小升序排列,链表头时间节点最小,依次递增。tick函数只处理链表中已经超时的节点,并打印buffer里的值。当休眠3秒时,链表中的前3个节点已经超时,剩下的2个节点没有超时,所以只打印前三个节点。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/385034.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

libevent学习笔记 一、基础知识

一、libevent是什么libevent是一个轻量级的开源的高性能的事件触发的网络库&#xff0c;适用于windows、linux、bsd等多种平台&#xff0c;内部使用select、epoll、kqueue等系统调用管理事件机制。它被众多的开源项目使用&#xff0c;例如大名鼎鼎的memcached等。特点&#xff…

汉字逆置

在计算机中&#xff0c;一个汉字用无法用1个字节来表示 #include<stdio.h> int main() {char buf[256] "你好";int len 0;while(buf[len]);len--;printf("%d\n",len);// 4一个汉字两个字节 //printf("%p\n",buf);return 0; } 在windows下…

libevent项目分析(一) -- 准备阶段

项目的简介 我理解libevent是一个轻量级的&#xff0c;跨平台高效的&#xff08;C语言实现&#xff09;事件驱动库&#xff0c;类似于ACE项目中的ACE_Reactor&#xff0c;它实现了网络通讯套接口I/O事件&#xff0c;定时器事件&#xff0c;信号事件的监听和事件处理函数回调机制…

混合字符串字符数统计

因为汉字占一个以上字节&#xff0c;如何统计一个既有汉字又有字母的字符串呢&#xff1f; 汉字在计算机中的ASCII是以负数来与其他普通字符的ASCII区分的。 #include<stdio.h> int main() {char buf[256] "你好世界";printf("%d\n",buf[0]); //-60…

清除字符串空格

1.清除字符串中右边的空格 从字符串尾部开始&#xff0c;找到非空格处&#xff0c;将下一个字符置为0即可。 //清除右边空格 #include<stdio.h> int main() {char buf[] "hello world ";int len 0;//calculate the length of stringwhile(buf[len]);le…

浅谈auto_ptr智能指针

引入智能指针&#xff1a;智能指针的实现原理&#xff1a; 资源分配即初始化RAII(Resource Acquisition Is Initialization)&#xff1a; 定义一个类来封装资源的分配和释放&#xff0c;在构造函数完成资源的分配和初始化&#xff0c;在析构函数完成资源的清理&#xff0c;可…

随机数

随机数产生器rand(),头文件为#include<stdlib.h> #include<stdio.h> #include<stdlib.h>int main() {int value;int i;for(i 0; i < 10; i){value rand();printf("value %d\n",value);}return 0; } 运行结果&#xff1a; value 41 value 1…

多重继承之虚继承(主要是为了解决产生的数据冗余问题)

虚继承 是面向对象编程中的一种技术&#xff0c;是指一个指定的基类&#xff0c;在继承体系结构中&#xff0c;将其成员数据实例共享给也从这个基类型直接或间接派生的其它类。形式&#xff1a;在继承定义中包含了virtual关键字的继承关系&#xff0c;如下图中&#xff0c;类A就…

通过syslog接收远程日志

通过syslog接收远程日志通过syslog接收远程主机的日志&#xff0c;需要做一些环境配置。客户机A通过syslog将日志信息发送到服务主机B&#xff08;或称日志采集服务器&#xff09;。以下说明配置过程&#xff08;我的实验环境是&#xff0c;客户机A&#xff1a;Solaris 10&…

linux syslog服务器配置,自动发日志到另一台日志服务器

1.客户端:168.1.20.66修改/etc/syslog.conf 添加syslog.info 168.1.80.302.日志服务器:168.1.80.30修改/etc/sysconf/syslog 修改SYSLOGD_OPTIONS为 "-r -x -m 0" #-r表示允许接收外来的消息&#xff0c;-x表示不解析DNS, #-m 0表示时间戳标记间隔,如果指定只接…

Make文件(一)

基本规则&#xff1a; 目标&#xff1a;依赖 &#xff08;tab&#xff09;规则 目标&#xff1a;需要生成的目标文件 依赖&#xff1a;生成该目标所需的一些文件 规则&#xff1a;由依赖文件生成目标文件的手段 tab&#xff1a;每条规则前必须以tab开头&#xff0c;使用空格不行…

移植驱动完毕后加载时的version magic报错原因以及解决办法

History:2012-02-17Author:yingru移植rt3070的AP驱动到装有fedora14的PC机上时&#xff0c;模块编译完毕后&#xff0c;加载时提示invalid module format。PC机环境介绍&#xff1a;内核版本&#xff1a;2.6.35.6-45.fc14.i686命令行输入dmesg查看最后的日志&#xff0c;发现如…

/proc 虚拟文件系统(实例)

Linux下有一个神奇的目录/proc&#xff0c;经常会运行 cat /proc/cpuinfo 命令查看cpu信息&#xff0c;/proc下的确有cpuinfo文件&#xff0c;但是这个文件不是物理存在的&#xff0c;是软件虚拟出来的&#xff0c;与普通文件不同&#xff0c;该文件是动态的。通过/proc可以实现…

内核模块中对文件的读写

平时网络部分的东西碰的多些&#xff0c;这块一开始还真不知道怎么写&#xff0c;因为肯定和在用户空间下是不同的。google过后&#xff0c;得到以下答案。一般可以用两种方法&#xff1a;第一种是用系统调用。第二种方法是filp->open()等函数。下面分别来说下这两种方法。1…

Makefile文件试错

1成功&#xff1a; src $(wildcard ./*cpp) obj $(patsubst %.cpp,%.o ,$(src))target test$(target) : $(obj)g $(obj) -o $(target) -I/usr/include/mysql -L/usr/lib/mysql/ -lmysqlclient %.o: %.cppg -c $< -o $ -I/usr/include/mysql -L/usr/lib/mysql/ -lmysql…

内核定时器timer_list使用

Linux内核中提供了timer使用的API&#xff0c;做一个简单的记要。 1. 包含的头文件&#xff1a;linux/timer.h 2. 数据类型&#xff1a;struct timer_list; 包含的主要成员&#xff1a; a. data:传递到超时处理函数的参数&#xff0c;主要在多个定时器同时使用时&#xff0c;区…

内存四区

1.代码区&#xff1a; 代码区Code&#xff0c;程序被操作系统加载到内存的时候&#xff0c;所有的可执行代码都加载到代码区&#xff0c;也叫代码段&#xff0c;这块内存是不可以在运行期间修改的。 2. 静态区 所有的全局变量以及程序中的静态变量都存储在静态区。 #include<…

最高效的进(线)程间通信机制--eventfd

我们常用的进程&#xff08;线程&#xff09;间通信机制有管道&#xff0c;信号&#xff0c;消息队列&#xff0c;信号量&#xff0c;共享内存&#xff0c;socket等等&#xff0c;其中主要作为进程&#xff08;线程&#xff09;间通知/等待的有管道pipe和socketpair。线程还有特…

malloc,calloc,realloc

与堆操作相关的两个函数 malloc #include<stdio.h> #include<stdlib.h> #include<string.h>int main() {char *p malloc(10); //内存随机&#xff0c;未做处理int i;for(i 0; i < 10: i){printf(“%d “,p[i]);} free(p);return 0; } 运行结果&…

Linux内核同步机制之completion

内核编程中常见的一种模式是&#xff0c;在当前线程之外初始化某个活动&#xff0c;然后等待该活动的结束。这个活动可能是&#xff0c;创建一个新的内核线程或者新的用户空间进程、对一个已有进程的某个请求&#xff0c;或者某种类型的硬件动作&#xff0c;等等。在这种情况下…