APUE 习题10.5 - Implementing Software Timer

这是我自己实现的版本,参考 Don Libes 的 Implementing Software Timer 我是用alarm实现的,好像不太精确,还有一个版本可以设定定时器的次数,可是随着次数增多精确度就成了不可忽略的问题了。不排除有很多BUG, 而且有些细节没考虑。
摘要由CSDN通过智能技术生成

这是我自己实现的版本,参考 Don Libes 的 Implementing Software Timer

我是用alarm实现的,好像不太精确,还有一个版本可以设定定时器的次数,可是随着次数增多精确度就成了不可忽略的问题了。

不排除有很多BUG, 而且有些细节没考虑。

/************** timers.h *************/
/* 
 * Title: Answer to excersice 10.5 of APUE - Software timer
 * By: Leon
 *
 * This implementation is similar to Don Libes's, and I modify
 * it according to my thought. This timer is not very precise.
 */

#ifndef _APUE_TIMER_
#define _APUE_TIMER_

#include <stdio.h>
#include <limits.h>
#include <signal.h>
#include <sys/times.h>
#include <time.h>
#include <assert.h>
#include <unistd.h>

#define TRUE    1
#define FALSE    0

#define MAX_TIMERS     128         /* number of timers */
#define VERY_LONG_TIME     INT_MAX      /* longest time possible */

/* 
 * no return value, the function takes 
 * only one parameter for general purpose.
 */
typedef void timer_handler(void *arg);    

struct __apue_timer
{    
    int        inuse;        /* TRUE if in use */
    int        time_wait;    /* relative time to wait */
    timer_handler    *handler;    /* called when the timer has expired */
    void        *arg;        /* argument of handler */
};

typedef struct __apue_timer ATIMER;

/* 
 * initialize the timers array.
 * This function must be called first.
 * returns 0 on success and -1 on error.
 */
int timer_init(void);

/* 
 * stop and cancel all timers.
 * after this function was called, don't use
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值