[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: ordering of printed lines changes when redirecting
From: |
walter harms |
Subject: |
Re: ordering of printed lines changes when redirecting |
Date: |
Mon, 18 Jul 2016 16:11:02 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.1.16) Gecko/20101125 SUSE/3.0.11 Thunderbird/3.0.11 |
Am 18.07.2016 15:59, schrieb Greg Wooledge:
> On Mon, Jul 18, 2016 at 10:22:46AM +0200, walter harms wrote:
>> ( ./a.out 2>&1 )
>> hallo 5
>> hallo 6
>> hallo 7
>> hallo 8
>> hallo 9
>
> (snip)
>
>> ./a.out >xx 2>&1
>> cat xx
>> hallo 6
>> hallo 8
>> hallo 5
>> hallo 7
>> hallo 9
>
> Looks like an artifact of stdio (libc) buffering. When stdout and
> stderr are going to a terminal (first example), you get line buffering
> (flushed after each newline), and thus the order you expect. When stdout
> and stderr are going to a file, you get much larger buffers. Looks like
> the flush happens implicitly when the program exits. In your second
> example here, you happen to get all of the stderr lines first, followed
> by all of the stdout lines.
>
> See if the behavior changes when you add these lines to the start of
> the C program:
>
> setvbuf(stdout, NULL, _IOLBF, 0);
> setvbuf(stderr, NULL, _IOLBF, 0);
thx for the fast reply, my testprogramm works as expected after the changes.
My basic mistake was that my understand was that \n should flush the buffer,
that was obviously wrong.
NTL i would recommend to add one or two line about buffering into
the ducomentation :)
thx for help,
re,
wh