On 18/07/16 14:59, Greg Wooledge wrote: > 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);
One might be able to control from outside the program as well like: stdbuf -oL your_prog