[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Modify buffering of standard streams via environment variables (not
From: |
Carl Edquist |
Subject: |
Re: Modify buffering of standard streams via environment variables (not LD_PRELOAD)? |
Date: |
Sat, 20 Apr 2024 16:45:34 -0500 (CDT) |
On Sat, 20 Apr 2024, Zachary Santer wrote:
This was actually in RHEL 7.
Oh. In that case it might be worth looking into ...
I don't know how buffering works when stdout and stderr get redirected
to the same pipe. You'd think, whatever it is, it would have to be smart
enough to keep them interleaved in the same order they were printed to
in. That in mind, I would assume they both get placed into the same
block buffer by default.
My take is always to try it and find out. Though in this case I think the
default (without using stdbuf) is that the program's stderr is output to
the pipe immediately (ie, unbuffered) on each library call (fprintf(3),
fputs(3), putc(3), fwrite(3)), while stdout is written to the pipe at
block boundaries - even though fd 1 and 2 refer to the same pipe.
If you force line buffering for stdout and stderr, that is likely what you
want, and it will interleave _lines_ in the order that they were printed.
However, stdout and stderr are still separate streams even if they refer
to the same output file/pipe/device, so partial lines are not interleaved
in the order that they were printed.
For example:
#include <stdio.h>
int main()
{
putc('a', stderr);
putc('1', stdout);
putc('b', stderr);
putc('2', stdout);
putc('c', stderr);
putc('3', stdout);
putc('\n', stderr);
putc('\n', stdout);
return 0;
}
will output "abc\n123\n" instead of "a1b2c3\n\n", even if you run it as
$ ./abc123 2>&1 | cat
or
$ stdbuf -oL -eL ./abc123 2>&1 | cat
...
Not that that's relevant for what you're doing :)
Carl