【发布时间】:2011-08-27 22:28:36
【问题描述】:
在我的情况下,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);
}
【问题讨论】:
-
但是函数 select() 的代码在哪里??
-
select是 UNIX 系统中的系统调用 -
你是如何获得
fd的?你确定它有效吗? -
我相信如果使用无效的 fd 调用 select 会返回 -1。在这种情况下,我通常使用 strace 来查看实际情况。
-
另外,您的问题标题和代码不匹配。你在哪里检查
select返回的值是否为0。它可能是1,在这种情况下,这意味着有数据要读取。
标签: c linux function networking select-function