disable stdout buffering ?

Discussion in 'C Programming' started by Mathias Herrmann, Oct 4, 2005.

  1. Hi.

    I have the following problem:
    Using popen() to execute a program and read its stdout works usually fine.
    Now I try to do this with a program called xsupplicant (maybe one knows),
    but I dont get the output of it while it is running.

    This is probably a problem of stdout being buffered, because if I use
    fflush() after a printf() in the xsupplicant then I can read the output.

    My question is now: is it possible to tell stdout not to buffer anything,
    but immediately print it out.

    I dont want to change the xsupplicant source code.

    Hope anyone understood what I am trying to say.


    thanks.
    Mathias Herrmann, Oct 4, 2005
    #1
    1. Advertising

  2. Mathias Herrmann

    Madhav Guest

    >
    > My question is now: is it possible to tell stdout not to buffer anything,
    > but immediately print it out.
    >

    Yes. Please see setvbuf and other related functions.

    Regards,
    Madhav.
    Madhav, Oct 4, 2005
    #2
    1. Advertising

  3. Thanks for your reply.

    On Tue, 04 Oct 2005 05:50:26 -0700, Madhav wrote:

    >>
    >> My question is now: is it possible to tell stdout not to buffer anything,
    >> but immediately print it out.
    >>

    > Yes. Please see setvbuf and other related functions.



    I tried setvbuf and setbuf:

    ....
    FILE* fp = popen("...","r");
    setvbuf(fp,NULL,_IONBF,0);
    ....

    But unfortunately it had no effect ...
    Mathias Herrmann, Oct 4, 2005
    #3
  4. Mathias Herrmann

    Richard Bos Guest

    Mathias Herrmann <herrmann@'remove_me'sit.fraunhofer.de> wrote:

    > On Tue, 04 Oct 2005 05:50:26 -0700, Madhav wrote:
    >
    > >> My question is now: is it possible to tell stdout not to buffer anything,
    > >> but immediately print it out.
    > >>

    > > Yes. Please see setvbuf and other related functions.

    >
    > I tried setvbuf and setbuf:
    >
    > ...
    > FILE* fp = popen("...","r");
    > setvbuf(fp,NULL,_IONBF,0);


    Ah. You are dealing with pipes, not with normal streams. Pipes are
    different; they may or may not respond to functions that work on files.
    Pipes are also not part of ISO C, and different implementations of pipes
    may behave in different ways. For a reliable answer, ask in a newsgroup
    for whatever library defines your pipes - probably POSIX, in which case,
    ask in comp.unix.programmer.

    Richard
    Richard Bos, Oct 4, 2005
    #4
  5. Mathias Herrmann

    Eric Sosman Guest

    Mathias Herrmann wrote On 10/04/05 09:22,:
    > Thanks for your reply.
    >
    > On Tue, 04 Oct 2005 05:50:26 -0700, Madhav wrote:
    >
    >
    >>>My question is now: is it possible to tell stdout not to buffer anything,
    >>>but immediately print it out.
    >>>

    >>
    >> Yes. Please see setvbuf and other related functions.

    >
    >
    >
    > I tried setvbuf and setbuf:
    >
    > ...
    > FILE* fp = popen("...","r");
    > setvbuf(fp,NULL,_IONBF,0);
    > ...
    >
    > But unfortunately it had no effect ...


    ... because it was done on "the wrong end" of the
    connection. You need to disable the buffering that
    the sender is doing, so you need to get the sender to
    call setvbuf() before doing anything else with its
    stdout stream.

    Unfortunately, that goes against your goal of not
    wanting to make any changes to the other program's code.
    However, the change is a very small one: just a one-liner
    at or near the start of main() will do it.

    Unfortunately (again), you're still at the mercy of
    the operating system. Telling the C library not to buffer
    an output stream causes fprintf() and the like to deliver
    characters directly to the O/S without undue delay, but
    doesn't govern what the O/S then chooses to do with them.
    You'll need to read your O/S' documentation on pipes (which
    aren't part of C, by the way).

    <off-topic>

    In my experience, many "interactive" programs fare
    rather poorly with pipes; the I/O model really isn't quite
    what's wanted. If your system provides pseudo-terminals
    (/dev/pty), they might be more appropriate. For further
    help along these lines -- or with pipes, for that matter --
    try comp.unix.programmer.

    </off-topic>

    --
    Eric Sosman, Oct 4, 2005
    #5
  6. > ... because it was done on "the wrong end" of the
    > connection. You need to disable the buffering that
    > the sender is doing, so you need to get the sender to
    > call setvbuf() before doing anything else with its
    > stdout stream.
    >
    > Unfortunately, that goes against your goal of not
    > wanting to make any changes to the other program's code.
    > However, the change is a very small one: just a one-liner
    > at or near the start of main() will do it.
    >
    > Unfortunately (again), you're still at the mercy of
    > the operating system. Telling the C library not to buffer
    > an output stream causes fprintf() and the like to deliver
    > characters directly to the O/S without undue delay, but
    > doesn't govern what the O/S then chooses to do with them.
    > You'll need to read your O/S' documentation on pipes (which
    > aren't part of C, by the way).
    >
    > <off-topic>
    >
    > In my experience, many "interactive" programs fare
    > rather poorly with pipes; the I/O model really isn't quite
    > what's wanted. If your system provides pseudo-terminals
    > (/dev/pty), they might be more appropriate. For further
    > help along these lines -- or with pipes, for that matter --
    > try comp.unix.programmer.
    >
    > </off-topic>



    Thanks a lot.
    Im going to try the use of a pty.
    Mathias Herrmann, Oct 4, 2005
    #6
  7. Mathias Herrmann wrote:
    >
    > Hi.
    >
    > I have the following problem:
    > Using popen() to execute a program and read its stdout works usually fine.
    > Now I try to do this with a program called xsupplicant (maybe one knows),
    > but I dont get the output of it while it is running.
    >
    > This is probably a problem of stdout being buffered, because if I use
    > fflush() after a printf() in the xsupplicant then I can read the output.
    >
    > My question is now: is it possible to tell stdout not to buffer anything,
    > but immediately print it out.
    >
    > I dont want to change the xsupplicant source code.


    You can't "unbuffer" xsupplicant's stdout without changing xsupplicant's
    source code, unless this ability is already built in.

    You could probably try something like this at the start of main():

    if ( !isatty(stdout) )
    setbuf(stdout,NULL);

    The specifics are probably OT to clc, so you might need to ask in a
    group where this would be on-topic. Perhaps comp.unix.programmer?

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Oct 4, 2005
    #7
  8. In article <pan.2005.10.04.12.11.37.993986@'remove_me'sit.fraunhofer.de>, Mathias Herrmann <herrmann@'remove_me'sit.fraunhofer.de> writes:
    >
    > My question is now: is it possible to tell stdout not to buffer anything,


    Yes:

    #include <stdio.h>

    int main(void)
    {
    int ret;
    ret = setvbuf(stdout, NULL, _IONBF, 0);
    if (ret == 0)
    puts("stdout is now unbuffered");
    return 0;
    }

    > but immediately print it out.


    No. While you can disable the C library's buffering for stdout
    (if setvbuf succeeds), you don't have any control over when the
    underlying system processes the output generated by the C library.

    However, I believe you've asked the wrong question, since you
    appear to want to change the stdout buffering for another process,
    without changing that program's source code. That you cannot do,
    in standard C. (Note that popen is not part of the standard C
    library.)

    I suggest you take this question to a newsgroup that deals with
    popen, such as comp.unix.programmer, or one specific to your
    operating system. (OT: Unless I am gravely mistaken, there's no
    POSIX/SUS mechanism to force another process to disable stdio
    buffering, either. Using a pseudo-tty rather than a pipe should
    get you line buffering rather than full buffering, though, which
    might suffice.)


    --
    Michael Wojcik

    You brung in them two expert birdwatchers ... sayin' it was to keep us from
    makin' dern fools of ourselfs ... whereas it's the inherent right of all to
    make dern fools of theirselfs ... it ain't a right held by you official types
    alone. -- Walt Kelly
    Michael Wojcik, Oct 4, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dh
    Replies:
    2
    Views:
    1,815
    Dario
    Feb 3, 2004
  2. Cecil Westerhof

    Non-buffering stdout

    Cecil Westerhof, Mar 25, 2008, in forum: Python
    Replies:
    0
    Views:
    256
    Cecil Westerhof
    Mar 25, 2008
  3. Chris McDonald
    Replies:
    5
    Views:
    262
    Kenny McCormack
    Feb 22, 2009
  4. Kaz Kylheku
    Replies:
    1
    Views:
    313
    Richard
    Feb 23, 2009
  5. Geoff Bache
    Replies:
    4
    Views:
    536
Loading...

Share This Page