gpt4 book ai didi

c - Select (Linux) 函数总是返回 0

转载 作者:太空狗 更新时间:2023-10-29 12:37:50 24 4
gpt4 key购买 nike

在我的例子中,Select 函数总是返回零,这是超时,并且这种情况持续发生,所以我的进程的 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);
}

最佳答案

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

关于c - Select (Linux) 函数总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6043080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com