定时器源代码C语言

源代码:

/*snmp_alarm.c*/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>

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

#include "snmp_alarm.h"

#define SNMP_MALLOC_STRUCT(s) (struct s *) calloc(1, sizeof(struct s)) 

static struct snmp_alarm *thealarms = NULL;
static int start_alarms = 0;
static unsigned int regnum = 1;

int init_alarm_post_config()
{
   
start_alarms = 1;
set_an_alarm();
return 0;
}
void sa_update_entry(struct snmp_alarm *a)
{
   



if(a->t_last.tv_sec == 0 && a->t_last.tv_usec == 0) {
   
        struct timeval t_now;
/* 
* Never been called yet, call time `t' from now. 
*/

        gettimeofday(&t_now, NULL);




        a->t_last.tv_sec = t_now.tv_sec;
        a->t_last.tv_usec = t_now.tv_usec;




        a->t_next.tv_sec = t_now.tv_sec + a->t.tv_sec;
        a->t_next.tv_usec = t_now.tv_usec + a->t.tv_usec;


        while(a->t_next.tv_usec >= 1000000) {
   

        a->t_next.tv_usec -= 1000000;

        a->t_next.tv_sec += 1;
        }
}




        else if(a->t_next.tv_sec == 0 && a->t_next.tv_usec == 0) {
   
/* 
* We've been called but not reset for the next call. 
*/
                if(a->flags & SA_REPEAT) {
   





                        if(a->t.tv_sec == 0 && a->t.tv_usec == 0) {
   
                                printf("update_entry: illegal interval specified\n");
                                snmp_alarm_unregister(a->clientreg);
                                return;
}
                        a->t_next.tv_sec = a->t_last.tv_sec + a->t.tv_sec;
                        a->t_next.tv_usec = a->t_last.tv_usec + a->t.tv_usec;

                        while(a->t_next.tv_usec >= 1000000) {
   
                                a->t_next.tv_usec -= 1000000;
                                a->t_next.tv_sec += 1;
                }
        }
        else {
   
/* 
* Single time call, remove it. 
*/
                snmp_alarm_unregister(a->clientreg);
                }
        }
}
void snmp_alarm_unregister(unsigned int clientreg
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值