I'm writing code that includes an option to get a tail -f effect, and it works fine. However I believe it is working rather inefficiently due to an unexpected detail of 4-arg select().

In the code, I open a number of files using:

sysopen $fh, $file, O_RDONLY | O_NONBLOCK
, then sit in a loop with:
$ract = $eact = $rvec; # vector of all filenos to read ($nfound, $timeleft) = select($ract, undef, $eact, $timer);
.. before using sysread() to get new data from the marked files.

This all works fine, except that each time round the loop select() returns saying that every one of the files is readable. On checking various manpages and Stevens I find that I've misunderstood select() - it is telling me not that there is data to read on the marked files, but that a read from this filehandle would not block, and in particular (Stevens): If we encounter the end of file on a descriptor, that descriptor is considered readable by select().

Looking at prior art, in File::Tail the author does lots of clever stuff to try to predict when a file is likely to have more data ready for read, and read from it only then; in the implementation of tail(1) for the ppt project (here) the author chooses not to retain open file handles at all, but instead repeatedly stat()s the file(s) to determine whether something has changed.

So, is there any reasonable workaround to this, to avoid repeatedly reading from filehandles that have no new data to supply? The intended use for this code will typically have one or more instances each tailing around 60 file descriptors on a production box, and if they can't spend most of their time sleeping in the kernel under select() I fear it will have a noticeable impact on performance of the machine as a whole.

Hugo


In reply to nonblocking I/O - testing whether file has more data to read by hv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.