Is it possible to force glibc to use the same buffer for stdout and stderr?

If I tee the both outputs:

./tests/tests 2>&1 | tee log

I get mixed messages (i.e. messages are ordered inside stdout and stderr but not with respect to each other). I can turn off the buffering:

stdbuf -i0 -o0 -e0 ./tests/tests 2>&1 | tee log

But it cause large slowdown (I'm pushing hundreds of MB of log). Is it possible to do have buffering but have it shared between stdout and stderr? FD 1 and 2 points to the same output (as in example).


I'm not entirely certain in what context you want to make the buffer the same, but in a C program, the author can use the setbuf() family of library functions to give stderr and stdout the same buffer.

Just usig the shell to invoke other programs, I don't think you can do this, for the reasons implied by the setbuf family of functions: the implementations of FILE *stderr and FILE *stdout have a per instance piece of memory (the buffer). If a program doesn't get written in a specific way, a way that sets the buffer to be the same piece of memory for stdout and stderr, then the shell can't make a program do that common buffering.

  • My reading of setbuf(3) is that they wouldn't share the state of buffer - only contents - hence bad things would happened (tm). However it is not stated explicitly. – Maciej Piechotka Nov 25 '12 at 16:43
  • I can read setbuf(3) and setvbuf(3) the same way you do. Now that you bring it up, I imagine that bad things would happen, that the equivalent of data race conditions occur. Maybe if you just did stderr = stdout; ? – Bruce Ediger Nov 25 '12 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.