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

Re: can you autoflush a program in unix?

by ikegami (Patriarch)
on Oct 20, 2009 at 18:47 UTC ( [id://802287]=note: print w/replies, xml ) Need Help??


in reply to can you autoflush a program in unix?

Yes and no.

The buffering in question is done entirely in the program. Unless the program gives you a means of controlling its flushing mechanism, you're out of luck.

However, most applications will automatically flush STDOUT (after each newline) when it's connected to a tty. You can fool such applications by using a pseudo tty. IPC::Run makes it easy to use a pseudo tty. See IO::Pty

Replies are listed 'Best First'.
Re^2: can you autoflush a program in unix?
by zwon (Abbot) on Oct 20, 2009 at 19:16 UTC
    However, most applications will autoflush STDOUT when it's connected to a tty.
    GNU libc by default opens stdout in line buffered mode if it's tty, and stderr is opened in unbuffered mode. The following code will demonstrate behavior:
    #include <stdio.h> #include <unistd.h> main() { printf("This is stdio output"); fprintf(stderr, "This is stderror output"); sleep(5); }
    On my Ubuntu it outputs "This is stderror output" and 5 seconds later "This is stdio output"
      Yes. Clarified my post.
Re^2: can you autoflush a program in unix?
by windowbreaker (Sexton) on Oct 20, 2009 at 19:02 UTC
    You could make the $FLUSH variable pull it's value from an environment variable like so:
    $FLUSH = $ENV{FLUSH} || 1;
    The 1 is a default, which you can change to 0. That lets the user call your script from the shell as:
    bash# AUTOFLUSH=1 /path/to/script.pl

      Aside from the discrepancy in your variable names, you seem to have missed this paragraph of the OP

      Now say for example that FLUSH is NOT set and I am unable to set FLUSH (for example if I don't have permission to modify to the program).

Re^2: can you autoflush a program in unix?
by Anonymous Monk on Oct 20, 2009 at 18:54 UTC
    you're out of luck

    Well not entirely, you're back to writing debuggers

      I don't understand. Why would one write a debugger rather than using an existing one, and what does that have to do with the topic at hand?
        Heh. I think he means use the Unix debugging API to, e.g. sniff for calls to fprintf() et al, and insert a fflush() call when that happens. Or something like that.

        Come to think of it, you could probably do what OP wants by writing an LD_PRELOAD library that overrides a large chunk of stdio... the caveats being that this is dark magic, ugly, will fail if the offending program is using its own buffered-output routines, and could conceivably make said program break in interesting ways.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found