Select (Linux) 函数始终返回 0

发布于 2024-11-07 19:31:22 字数 963 浏览 0 评论 0原文

在我的例子中,选择函数总是返回零,这是超时,而且这种情况持续发生,因此我的进程的 CPU 使用率也高达 98%。我也尝试设置 NULL 而不是设置一些超时值,但它仍然返回零。我还使用 poll 函数代替 select 。民意调查也出现了同样的问题。

这是我的代码的一部分;

while(1)
{        
    value = 0;
    selectTimeOut = 0;
    memset(buf,0,SIZE);
    FD_ZERO(&read_fds);
    FD_SET(fd, &read_fds);
    struct timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 1000;
    fdmax = fd;

    //using select to reduce cpu utilization
    selectret = select(fdmax + 1,&read_fds,NULL,NULL,&tv);
    if (selectret == -1)
    {
       print_sync("/home/fes/syclogs.txt","Select fails");
       exit(0);
    }
    else
    {
        print_sync("/home/fes/syclogs.txt","Error set is %s",strerror(errno));
        if(!FD_ISSET(fd, &read_fds))
        {
            print_sync("/home/fes/syclogs.txt","Select Time Out");
            selectTimeOut = 1;
        }
    }
    if(selectTimeOut == 1)
        continue;
    noread  = read(fd,buf,SIZE);
}

Select function in my case always returns zero, which is timeout and this is happening continuosly so my CPU usage also going upto 98 % for my process. I have also tried to set NULL instead of seting some timeout value , still it returns zero. I also used poll function replacing select. The same issue came with the poll also.

here is part of my code;

while(1)
{        
    value = 0;
    selectTimeOut = 0;
    memset(buf,0,SIZE);
    FD_ZERO(&read_fds);
    FD_SET(fd, &read_fds);
    struct timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 1000;
    fdmax = fd;

    //using select to reduce cpu utilization
    selectret = select(fdmax + 1,&read_fds,NULL,NULL,&tv);
    if (selectret == -1)
    {
       print_sync("/home/fes/syclogs.txt","Select fails");
       exit(0);
    }
    else
    {
        print_sync("/home/fes/syclogs.txt","Error set is %s",strerror(errno));
        if(!FD_ISSET(fd, &read_fds))
        {
            print_sync("/home/fes/syclogs.txt","Select Time Out");
            selectTimeOut = 1;
        }
    }
    if(selectTimeOut == 1)
        continue;
    noread  = read(fd,buf,SIZE);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-11-14 19:31:22

你的逻辑没有道理。仅当 select() 返回 -1 时,errno 才有意义。如果它返回零,则没有 fd 准备好,因此出现超时,并且无需测试其他任何内容。如果它返回正值,则需要循环并处理那么多就绪的 fd。

Your logic doesn't make sense. errno is only interesting if select() returns -1. If it returns zero, no fds were ready, so there was a timeout, and no need to test anything else. If it returns a positive value, you need to loop and process that many ready fd's.

听,心雨的声音 2024-11-14 19:31:22

为什么不检查文件结尾或类似情况?我相信 EOF 或描述符的其他特殊状态非常适合这种情况。

您可能应该进一步描述描述符和上下文。 fd从哪里来?它代表什么数据源?

查看您的调试消息,人们可能会得出这样的结论:您正在尝试监视常用文件的更改。我不认为 select 可以帮助完成这项任务。

Tail 实用程序源可能帮助您实现文件监控代码。

Why don't you check for end of file or similar condition? I believe EOF or other exceptional state of your descriptor is a perfect match for this situation.

You should probably further describe descriptor and the context. Where does fd comes from? What data source does it represent?

Looking at your debugging messages one may come to conclusion that you are trying to monitor changes of usual file. I don't think select can help with this task.

Tail utility source might help you to implement your file monitoring code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文