Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:24:04+00:00 2026-05-22T18:24:04+00:00

In the below code, I am attempting to perform a simple file tail operation.

  • 0

In the below code, I am attempting to perform a simple file tail operation. Assume foo.txt is completely empty. I would expect that the first can_read() would block but it does not. Nor do the can_read calls in sysreadline() block or wait either. Instead what happens is that the first can_read immediately returns the handle to foo.txt and the first can_read in sysreadline does the same. The sysread then returns nothing because there is nothing to read, which results in a busy wait inside sysreadline. How can this be? I know a select can end early due to a signal or closed file handle, but I don’t see any opportunity for that here. In fact, when text does appear (with a newline) in foo.txt, it is printed. I don’t see why the code doesn’t block indefinitely as the first can_read when there is nothing to read. In addition to wasting cpu, it makes it impossible to tail multiple files at the same time because you will always get stuck in the first busy wait. I feel like I must be overlooking something simple here…

This is perl 5.8.8



#!/usr/bin/perl -w
use strict;
use IO::Select;
use IO::Handle; 
use Symbol qw(qualify_to_ref); 

open my $inf, "<", "foo.txt" or die "hey! Can't open foo.txt!\n";
my $sel = IO::Select->new();
$sel->add($inf);

while(my @ready = $sel->can_read()){

    foreach my $handle (@ready){
    my $line = sysreadline($handle);
    print $line;
    }

}

##
## deal with buffering for select. from perl cookbook 7.23
sub sysreadline {
    my($handle, $timeout) = @_;
    $handle = qualify_to_ref($handle, caller( ));
    my $infinitely_patient = (@_ == 1 || $timeout new( );
    $selector->add($handle);
    my $line = "";
SLEEP:
    until (at_eol($line)) {
        unless ($infinitely_patient) {
            return $line if time( ) > ($start_time + $timeout);
        }
        # sleep only 1 second before checking again
        next SLEEP unless $selector->can_read(1.0);
INPUT_READY:
        while ($selector->can_read(0.0)) {
            my $was_blocking = $handle->blocking(0);
CHAR:       while (sysread($handle, my $nextbyte, 1)) {
                $line .= $nextbyte;
        ##print "next: [$nextbyte]\n";
                last CHAR if $nextbyte eq "\n";
            }
            $handle->blocking($was_blocking);
            # if incomplete line, keep trying
            next SLEEP unless at_eol($line);
            last INPUT_READY;
        }
    }
    return $line;
}
sub at_eol($) { $_[0] =~ /\n\z/ }

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:24:05+00:00Added an answer on May 22, 2026 at 6:24 pm

    The return from select means “read will not block (i.e. wait forever for some external event to happen)”, and not “data is available”. Reading files from disk never blocks, it returns 0 immediately at EOF.

    So you’re probably better off with File::Tail that @TLP suggests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how to modify the below code to strip =20
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
When running the below code a type is never returned, despite there being a
In the below code snippet can i replace char * to const char *
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
In the below code sample, what does {0:X2} mean? This is from the reflection
In the below code, the ListBox gets filled with the names of the colors
When retrieving objects from an NSMutableArray in cocoa-touch is the below code ok? Should
Okay so I have a scenario similar to the below code, I have a
The code below shows a sample that I've used recently to explain the different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.