bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ordering of printed lines changes when redirecting


From: Greg Wooledge
Subject: Re: ordering of printed lines changes when redirecting
Date: Mon, 18 Jul 2016 09:59:27 -0400
User-agent: Mutt/1.4.2.3i

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);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]