3

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);
}
rtruszk
  • 3,902
  • 13
  • 36
  • 53

2 Answers2

1

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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.

Community
  • 1
  • 1
Basilevs
  • 22,440
  • 15
  • 57
  • 102
  • He *is* checking for end of file 'or similar condition', not that there are similar conditions. If there is an EOS waiting to be read, the fd will be present in the read_fds set, so he won't see a select timeout and he will fall through to his read() call, where he can detect it (noread == 0). – user207421 May 19 '11 at 10:30
  • Well, I just can't imagine select timing out on usual file. – Basilevs May 19 '11 at 15:32
  • Not mentioning waiting on it. – Basilevs May 19 '11 at 15:45
  • I don't know what any of this is about. None of this makes sense. You ask why he isn't checking for EOS when he hasn't posted that part of the code. It's almost certainly not a 'usual file' but a socket: sockets have an EOS condition too, and conditions under which select() can time out. You don't appear to know what select() even is. – user207421 May 20 '11 at 02:07
  • now here fd is a named pipe fd ...i used mkfifo call ...and opened pipe using open in read mode only .....and from logs i have checked fd is a posotive no ..i mean a valid fd ..... – Sandeep Singh Phogat May 20 '11 at 05:51
  • @EJP, /home/fes/syclogs.txt seemed rather usual name for me. @user758885 is sending end connected to this pipe? Positiveness of fd just means successful opening, state of the pipe should be validated by other means. – Basilevs May 21 '11 at 05:20
  • @user758885, BTW select is interrupted by signals and BROKEN PIPE is one of them. – Basilevs May 21 '11 at 06:06
  • @Basilevs: (a) that's the name of his log file: there is zero evidence that that's the fd he is selecting on; (b) he has already told us he's selecting on a named pipe; (c) 'positiveness of the fd', if you mean FD_ISSET, means that the event he is selecting for has occurred; (d) the state of a pipe can be determined by select(): that's what it's for. You continue to make no sense. – user207421 May 21 '11 at 10:09
  • @EJP: a)That could be anything. b)Not until my second comment.c)No, I mean postiveness of value returned by open() - the thing OP talks about in his previous comment.d)State of abstract file descriptor can be anything, how can you describe it with such narrow interface? I prefer to think that select handles events, not states. – Basilevs May 21 '11 at 12:40
  • ok guys i have got the solution ....actually i opened pipe in RDONLY mode earlier ..now changed it to RDWR mode ....because from somewhere i got to know that pipe opened in RDONLY mode ...will always return read call succesfull , but returning a value = 0, to get rid of this u have to change it to RDWR mode ..... – Sandeep Singh Phogat May 22 '11 at 14:58
  • @Basilevs (a) exactly, so your remark about 'usual name' means nothing; (b) there was no prior evidence he was selecting on a file; indeed the presence of the filename in association with select() which works on fd's is an indication he *wasn't* selecting on that file; (c) and (d) a positive fd is valid, again I have no idea what this is about; (e) agree about select() delivering events, and (f) EOS does not cause select() to return zero. – user207421 May 23 '11 at 04:25