epoll_wait的timeout的误差

epoll_wait(timeout)会有一定的误差,一般是1/1000。

CentOS release 5.5 (Final)

Linux CCN-YZ-1-3n1 2.6.37-1 #2 SMP Fri Feb 25 14:50:02 CST 2011 x86_64 x86_64 x86_64 GNU/Linux

/**
build:
g++ epoll-wait-deviation.cpp -o epoll-wait-deviation -g -O0
./epoll-wait-deviation
*/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/epoll.h>

int64_t get_current_time(){
    timeval now;
    int ret = gettimeofday(&now, NULL);
    assert(ret != -1);

    return now.tv_sec * 1000 + now.tv_usec / 1000;
}

int main(int argc, char** argv){
    int ep = epoll_create(1024);
    if(ep == -1){
        printf("create epoll error!\n"); exit(-1);
    }
    
    for(int i = 0; i < 2 * 60 * 60 * 1000; i += 1000){
        int64_t start = get_current_time();
        
        epoll_event events[1];
        if(epoll_wait(ep, events, 1, i) == -1){
            printf("wait epoll error!\n"); exit(-1);
        }
        
        int64_t end = get_current_time();
        printf("epoll wait deviation=%d, timeout=%d (ms)\n", end - start - i, i);
    }
    
    close(ep);
    
    return 0;
}

结果如下:

epoll wait deviation=0, timeout=0 (ms)
epoll wait deviation=1, timeout=1000 (ms)
epoll wait deviation=4, timeout=2000 (ms)
epoll wait deviation=5, timeout=3000 (ms)
epoll wait deviation=4, timeout=4000 (ms)
epoll wait deviation=7, timeout=5000 (ms)
epoll wait deviation=6, timeout=6000 (ms)
epoll wait deviation=8, timeout=7000 (ms)
epoll wait deviation=9, timeout=8000 (ms)
epoll wait deviation=10, timeout=9000 (ms)
epoll wait deviation=11, timeout=10000 (ms)
epoll wait deviation=12, timeout=11000 (ms)
epoll wait deviation=14, timeout=12000 (ms)
epoll wait deviation=13, timeout=13000 (ms)
epoll wait deviation=16, timeout=14000 (ms)
epoll wait deviation=16, timeout=15000 (ms)
epoll wait deviation=17, timeout=16000 (ms)
epoll wait deviation=19, timeout=17000 (ms)
epoll wait deviation=19, timeout=18000 (ms)
epoll wait deviation=20, timeout=19000 (ms)
epoll wait deviation=20, timeout=20000 (ms)
epoll wait deviation=22, timeout=21000 (ms)
epoll wait deviation=24, timeout=22000 (ms)
epoll wait deviation=23, timeout=23000 (ms)
epoll wait deviation=26, timeout=24000 (ms)
epoll wait deviation=26, timeout=25000 (ms)
epoll wait deviation=27, timeout=26000 (ms)
epoll wait deviation=28, timeout=27000 (ms)
epoll wait deviation=29, timeout=28000 (ms)
epoll wait deviation=30, timeout=29000 (ms)
epoll wait deviation=32, timeout=30000 (ms)
epoll wait deviation=31, timeout=31000 (ms)
epoll wait deviation=34, timeout=32000 (ms)
epoll wait deviation=34, timeout=33000 (ms)
epoll wait deviation=36, timeout=34000 (ms)
epoll wait deviation=36, timeout=35000 (ms)
epoll wait deviation=37, timeout=36000 (ms)
epoll wait deviation=39, timeout=37000 (ms)
epoll wait deviation=40, timeout=38000 (ms)
epoll wait deviation=39, timeout=39000 (ms)
epoll wait deviation=41, timeout=40000 (ms)
epoll wait deviation=43, timeout=41000 (ms)
epoll wait deviation=43, timeout=42000 (ms)
epoll wait deviation=44, timeout=43000 (ms)
epoll wait deviation=45, timeout=44000 (ms)
epoll wait deviation=47, timeout=45000 (ms)
epoll wait deviation=47, timeout=46000 (ms)
epoll wait deviation=48, timeout=47000 (ms)
epoll wait deviation=49, timeout=48000 (ms)
epoll wait deviation=50, timeout=49000 (ms)
epoll wait deviation=52, timeout=50000 (ms)
epoll wait deviation=52, timeout=51000 (ms)
epoll wait deviation=53, timeout=52000 (ms)
epoll wait deviation=55, timeout=53000 (ms)
epoll wait deviation=56, timeout=54000 (ms)
epoll wait deviation=57, timeout=55000 (ms)
epoll wait deviation=57, timeout=56000 (ms)
epoll wait deviation=57, timeout=57000 (ms)
epoll wait deviation=59, timeout=58000 (ms)
epoll wait deviation=61, timeout=59000 (ms)
epoll wait deviation=61, timeout=60000 (ms)
epoll wait deviation=62, timeout=61000 (ms)
epoll wait deviation=63, timeout=62000 (ms)
epoll wait deviation=64, timeout=63000 (ms)
epoll wait deviation=65, timeout=64000 (ms)
epoll wait deviation=66, timeout=65000 (ms)
epoll wait deviation=67, timeout=66000 (ms)
epoll wait deviation=68, timeout=67000 (ms)
epoll wait deviation=69, timeout=68000 (ms)
epoll wait deviation=71, timeout=69000 (ms)
epoll wait deviation=71, timeout=70000 (ms)
epoll wait deviation=72, timeout=71000 (ms)
epoll wait deviation=73, timeout=72000 (ms)
epoll wait deviation=74, timeout=73000 (ms)
epoll wait deviation=75, timeout=74000 (ms)
epoll wait deviation=76, timeout=75000 (ms)
epoll wait deviation=77, timeout=76000 (ms)
epoll wait deviation=79, timeout=77000 (ms)
epoll wait deviation=79, timeout=78000 (ms)
epoll wait deviation=81, timeout=79000 (ms)
epoll wait deviation=81, timeout=80000 (ms)
epoll wait deviation=82, timeout=81000 (ms)
epoll wait deviation=83, timeout=82000 (ms)
epoll wait deviation=84, timeout=83000 (ms)
epoll wait deviation=85, timeout=84000 (ms)
epoll wait deviation=86, timeout=85000 (ms)
epoll wait deviation=87, timeout=86000 (ms)
epoll wait deviation=88, timeout=87000 (ms)
epoll wait deviation=89, timeout=88000 (ms)
epoll wait deviation=90, timeout=89000 (ms)
epoll wait deviation=91, timeout=90000 (ms)
epoll wait deviation=92, timeout=91000 (ms)
epoll wait deviation=93, timeout=92000 (ms)
epoll wait deviation=94, timeout=93000 (ms)
epoll wait deviation=95, timeout=94000 (ms)
epoll wait deviation=97, timeout=95000 (ms)
epoll wait deviation=96, timeout=96000 (ms)
epoll wait deviation=98, timeout=97000 (ms)
epoll wait deviation=99, timeout=98000 (ms)
epoll wait deviation=100, timeout=99000 (ms)
epoll wait deviation=101, timeout=100000 (ms)
epoll wait deviation=101, timeout=101000 (ms)
epoll wait deviation=101, timeout=102000 (ms)
epoll wait deviation=101, timeout=103000 (ms)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

winlinvip

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

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

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

打赏作者

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

抵扣说明:

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

余额充值