epoll 定时器

参考:

Linux下使用epoll监听定时器-CSDN博客

但是这个用的是gettimeofday。

本人使用的是

复制代码
#include <stdlib.h>
#include<stdio.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdint.h>

void timer_expires(void)
{
    struct timespec curr;
    static struct timespec start;
    static int first_call = 1;
    if (first_call)
    {
        first_call = 0;
        if (clock_gettime(CLOCK_MONOTONIC, &start) == -1)
            perror("clock_gettime");
    }
    if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1)
        perror("clock_gettime");
    int secs, nsecs;
    secs = curr.tv_sec - start.tv_sec;
    nsecs = curr.tv_nsec - start.tv_nsec;
    if (nsecs < 0)
    {
        secs--;
        nsecs += 1000000000;
    }
    printf("%d.%03d \n", secs, (nsecs + 500000) / 1000000);  //四舍五入纳秒转换为毫秒
}

int main(int argc, char const *argv[])
{
    int timefd = timerfd_create(CLOCK_MONOTONIC, 0);
    if (timefd == -1)
    {
        perror("timerfd_create failed");
    }

    struct itimerspec new_value = {};
    new_value.it_value.tv_sec = 1;
    new_value.it_value.tv_nsec = 0;

    new_value.it_interval.tv_sec = 5;
    new_value.it_interval.tv_nsec = 0;

    if (timerfd_settime(timefd, 0, &new_value, NULL) == -1)
    {
        perror("timerfd_settime failed");
    }
    printf("time start-----------\n");
    timer_expires();
    /*-------------------------------------------------------------------------*/
    struct epoll_event ev;
    ev.data.fd = timefd;
    ev.events = EPOLLIN; // 文件描述符可读的时候就触发

    int epollfd = epoll_create(EPOLL_CLOEXEC);
    if (epollfd == -1)
    {
        perror("epoll_create failed");
    }

    epoll_ctl(epollfd, EPOLL_CTL_ADD, timefd, &ev);

    /* int epoll_wait(int epfd,struct epoll_event * events, int maxevents,int timeout);
         epfd :epoll文件描述符
  events :接口的返回参数,一般都是一个数组,数组长度大于等于maxevents。
  maxevents:期望捕获的事件的个数。
  timeout :超时时间(>=0),单位是毫秒ms,-1表示阻塞,0表示不阻塞
   */
    const int maxEvents = 1; // 必须大于1
    struct epoll_event events[maxEvents];
    int buf[2];
    int ret = 0;
    int i =0;
    while (1)
    {
        int nfd = epoll_wait(epollfd, events, maxEvents, -1);
        if (nfd == -1)
            perror("epoll_wait failed");
        for (i = 0; i < nfd; ++i)
        {
            ret = read(events[i].data.fd, buf, sizeof(buf));
            if (ret < 0)
                perror("epoll callback read error");
            timer_expires();
        }
    }

    return 0;
}

会发现有0.1 的输出,是因为四舍五入的原因,如果要很精确,建议用gettimeofday(未处理)

在Linux中常用的时间结构有struct timespec 和struct timeval 。

下面是两个结构的定义

struct timespec

{

__time_t tv_sec; /* Seconds. */

long tv_nsec; /* Nanoseconds. */

};

struct timeval {

   time_t tv_sec;

   suseconds_t tv_usec;

};

两者的区别是timespec的第二个参数是纳秒数,而timeval的第二个参数是毫秒数。

相关推荐
溜达的大象20 分钟前
服务器挂了等用户报障?我用Prometheus搭了一套监控告警,服务器出状况第一时间通知我
服务器·php·prometheus
ylscode40 分钟前
Comodo防火墙曝致命零日漏洞:单个IPv6数据包即可触发Windows蓝屏死机
运维·网络·windows·安全·安全威胁分析
实在智能RPA1 小时前
药企GMP合规自动化破局:实在Agent的功能完整度评估与落地实践
运维·人工智能·ai·自动化
Shota Kishi1 小时前
SLV 新增 allnodes-jito 支持:Solana 验证者多客户端运维与 AI agent 自动化解析
运维·自动化·区块链
闪电悠米2 小时前
黑马点评-Redisson-01_why_redisson
java·服务器·网络·数据库·缓存·wpf
hj2862513 小时前
linux下一步学习内容
linux·运维
睡不醒男孩0308233 小时前
数据库高可用运维实操指南:基于CLup的PostgreSQL生产环境自动化管理
运维·数据库·postgresql
V搜xhliang02464 小时前
临床科研新范式:从选题到投稿,AI智能体如何接管全流程?
运维·数据结构·人工智能·算法·microsoft·数据挖掘·自动化
tudoSearcher4 小时前
日志、指标、链路追踪:可观测性三支柱深度解析
运维·服务器·网络·prometheus
xier_ran4 小时前
【infra之路】Linux基础命令与系统排查
linux·运维·服务器