Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

interrupt buffered output

by Errto (Vicar)
on Jan 19, 2007 at 20:04 UTC ( [id://595561]=perlquestion: print w/replies, xml ) Need Help??

Errto has asked for the wisdom of the Perl Monks concerning the following question:

If I am writing to a file using normal buffered PerlIO, which at least on my system (Perl 5.8.6 for Win32) seems to use 4k buffers, and I interrupt the program with Ctrl-C, will it flush the buffers? If not, is there anything I can do to coax it into doing so? Cheers,

Replies are listed 'Best First'.
Re: interrupt buffered output
by BrowserUk (Patriarch) on Jan 19, 2007 at 20:20 UTC

    This will attempt to close (and therefore flush) all open files (with filenos 0 .. 255) when ^C is received:

    require POSIX; $SIG{INT} = sub{ warn 'Interupted'; POSIX::close( $_ ) for 0 .. 255; exit; };

    You might also want to install the same signal handler for ^Break also:

    require POSIX; $SIG{BREAK} = $SIG{INT} = sub{ warn 'Interrupted'; POSIX::close( $_ ) for 0 .. 255; exit; };

    If you are opening and closing many files then you might need to increase the range. The limit will depend upon who/how your copy of perl was built. With the version of AS perl on my system the limit appears to be 2047 concurrent handles. I'm not sure if that information is available via Config somewhere?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      I'm not sure if that information is available via Config somewhere?

      BSD::Resource's getrlimit (if your platform supports it).

      -derby
Re: interrupt buffered output
by shmem (Chancellor) on Jan 19, 2007 at 20:10 UTC
    You could install a signal handler that closes your open files.
    Closing files will flush their buffers, afaik.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: interrupt buffered output
by zentara (Archbishop) on Jan 19, 2007 at 20:13 UTC
    I think the command "sync" should do it, but no gaurantees. There was just a lively discussion about this in the newsgroups recently, where someone wanted to test the read-write speeds of a hard drive, and found that sync will sometimes pull a read out of the buffers, if it still exists, so he wasn't really reading from the drive. Talk was that he needed to flush all unused ram to force it out , and something about "future kernel version, etc. :-) )

    But in your case, I think sync will suffice.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://595561]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found