Select (Linux) function always returns 0
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;
mem开发者_开发知识库set(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);
}
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.
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.
精彩评论