Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix timerfd在读取()时神秘地将int设置为0_Unix_Timer - Fatal编程技术网

Unix timerfd在读取()时神秘地将int设置为0

Unix timerfd在读取()时神秘地将int设置为0,unix,timer,Unix,Timer,我在ubuntu 14.04中做了一个timerfd hello world,但遇到了一个奇怪的情况:读取timerfd后,int计数被重置,但uint64_int没有重置 int main(int agrc, char **argv) { unsigned int heartbeat_interval = 1; struct itimerspec next_timer; struct timespec now; if (clock_gettime(CLOCK_

我在ubuntu 14.04中做了一个timerfd hello world,但遇到了一个奇怪的情况:读取timerfd后,int计数被重置,但uint64_int没有重置

int main(int agrc, char **argv) {
    unsigned int heartbeat_interval = 1;
    struct itimerspec next_timer;
    struct timespec now;

    if (clock_gettime(CLOCK_REALTIME, &now) == -1)
        err_sys((WHERE + std::string("timer error")).c_str());
    next_timer.it_value.tv_sec = now.tv_sec;
    next_timer.it_value.tv_nsec = 0;
    next_timer.it_interval.tv_sec = heartbeat_interval;
    next_timer.it_interval.tv_nsec = 0;
    int timefd = timerfd_create(CLOCK_REALTIME, 0);
    if (timerfd_settime(timefd, TFD_TIMER_ABSTIME, &next_timer, NULL) == -1) {
        err_sys((WHERE).c_str());
    }
    uint64_t s;
    int exp;
    int count = 1;
    uint64_t count1=0;
    while (1) {
        s = read(timefd, &exp, sizeof(uint64_t));
        if (s != sizeof(uint64_t)) {
            err_sys((WHERE).c_str());
        }
    }
}
除非您的
int
uint64\t
类型大小相同,否则这是一个非常糟糕的主意。最可能发生的情况是,您正在读取的64位正在覆盖
exp
以及堆栈上它旁边的任何内容

事实上,即使它们大小相同,这也不是个好主意。你应该拥有的是:

s = read(timefd, &exp, sizeof(exp));
这样,您就可以保证永远不会覆盖数据,下一行将为您解决问题:

if (s != sizeof(uint64_t)) {
它不会解决无符号整数类型和整数类型将被区别对待的问题,但是您可以通过为
exp
使用正确的类型来解决这个问题

除非您的
int
uint64\t
类型大小相同,否则这是一个非常糟糕的主意。最可能发生的情况是,您正在读取的64位正在覆盖
exp
以及堆栈上它旁边的任何内容

事实上,即使它们大小相同,这也不是个好主意。你应该拥有的是:

s = read(timefd, &exp, sizeof(exp));
这样,您就可以保证永远不会覆盖数据,下一行将为您解决问题:

if (s != sizeof(uint64_t)) {
它不会解决无符号整数类型和整数类型将被区别对待的问题,但是您可以通过为
exp
使用正确的类型来解决这个问题