Linux定时器与Epoll探究

  • ailx10:这里我想让程序定时2秒,打印一个tick,我要怎么做?
  • 大黑客:这不很简单吗?
  • ailx10:sleep就是个弟弟,能不能不用sleep做定时器?
  • 大黑客:不用sleep怎么定时啊?
  • ailx10:Epoll + timerfd
  • 大黑客:要不要这么专业啊?
  • ailx10: 嘿嘿,我可是做hacker的男人,能不专业吗?
  • 大黑客:那就开始你的表演吧 ...

e5bd6bc693e8d6b7c473a7a6581d0f18.png

这里有一个坑,我在写timer定时器的时候,发现read函数返回-1,错误码errno=22,说是参数错误,你绝对想不到第2个参数必须是uint64_t类型。

read的函数原型:(大雾!定时器这里需要小心)

#include <unistd.h>
ssize_t read(int filedes, void *buf, size_t nbyetes);

主函数:

// main.c by ailx10
#include <stdio.h>
#include "epoll.h"
#include "timer.h"

int main()
{
  ep_init();
  timer_cre();
  ep_wait();
  return 0;
}

Epoll事件驱动函数:

// epoll.c by ailx10
#include <stdio.h>
#include <sys/epoll.h>
#include "epoll.h"

static int g_epfd = 0;

void ep_init()
{
   int epfd = epoll_create(1);
   g_epfd = epfd;
   return ;
}

void ep_add(ep_entry_s* p)
{
  struct epoll_event stEpEvent;
  stEpEvent.events = EPOLLIN | EPOLLHUP | EPOLLERR;
  stEpEvent.data.ptr = (void*)p;
  epoll_ctl(g_epfd,EPOLL_CTL_ADD,p->fd,&stEpEvent);
  return;
}

void ep_del(ep_entry_s* p)
{
  struct epoll_event stEpEvent;
  stEpEvent.events = EPOLLIN | EPOLLHUP | EPOLLERR;
  stEpEvent.data.ptr = (void*)p;
  epoll_ctl(g_epfd,EPOLL_CTL_DEL,p->fd,&stEpEvent);
  return;
}

void ep_exit()
{
  g_epfd = -1;
  return;
}

void ep_wait()
{
  int fd;
  int num;
  ep_entry_s* pstp;
  ep_callback_func fcallback;
  struct epoll_event astEvent[1];
  while(1)
  {
    fd = epoll_wait(g_epfd,astEvent,1,-1);
    for(num = 0; num < fd;++num)
    {
      pstp = (ep_entry_s*)astEvent[num].data.ptr;
      fcallback = pstp->fCallback;
      fcallback(astEvent[num].events,pstp->fd);
    }
  }
}

对应的头文件:

// epoll.h by ailx10
#include <stdio.h>

typedef int (*ep_callback_func)(int event,int fd);

typedef struct tagEpEntry
{
  int fd;
  ep_callback_func fCallback;
}ep_entry_s;

void ep_init();
void ep_add(ep_entry_s* p);
void ep_del(ep_entry_s* p);
void ep_exit();
void ep_wait();

定时器函数:

// timer.c by ailx10
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include "epoll.h"
#include "timer.h"

static ep_entry_s g_ep_entry = {-1,NULL};

static int _callback(int event,int fd)
{
  uint64_t count = 0;
  if(0 != (EPOLLIN & event))
  {
    int n =read(fd,(void*)&count,sizeof(count));
    //printf("%d\n",n);
    if (n > 0)
    {
      printf("tick.\n");
    }
  }
  return 0;
}

void timer_cre()
{
  struct itimerspec timer;
  int timerfd = timerfd_create(CLOCK_MONOTONIC,0);
  printf("timefd=%d\n",timerfd);
  memset(&timer,0,sizeof(timer));
  timer.it_interval.tv_sec = 2;
  timer.it_value.tv_sec = 2;
  timerfd_settime(timerfd,0,&timer,NULL);
  g_ep_entry.fd = timerfd;
  g_ep_entry.fCallback = (ep_callback_func)_callback;
  ep_add(&g_ep_entry);
  return;
}

void _timer_del()
{
  ep_del(&g_ep_entry);
  close(g_ep_entry.fd);
  g_ep_entry.fCallback = NULL;
  return;
}

对应的头文件:

// timer.h by ailx10
#include <stdio.h>

void timer_cre();


谢谢阅读 ~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ailx10

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值