Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer 1

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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