In an application to be run on Linux I want every output character
to be flushed immediately through stdout onto the screen.
Currently I have a couple explicit fflush() calls in my C program.
I wanted to avoid these calls and configure the output channel on
OS level to immediately output any arriving character automatically.
It seems I need something like the tcsetattr(3) 'FLUSHO', but the
man page says "FLUSHO (not in POSIX; not supported under Linux)".
What would then be the way to achieve that on Linux (if possible)?
In an application to be run on Linux I want every output character
to be flushed immediately through stdout onto the screen.
Currently I have a couple explicit fflush() calls in my C program.
I wanted to avoid these calls and configure the output channel on
OS level to immediately output any arriving character automatically.
It seems I need something like the tcsetattr(3) 'FLUSHO', but the
man page says "FLUSHO (not in POSIX; not supported under Linux)".
What would then be the way to achieve that on Linux (if possible)?
Janis
In an application to be run on Linux I want every output character to be flushed immediately through stdout onto the screen.
In an application to be run on Linux I want every output character
to be flushed immediately through stdout onto the screen.
Currently I have a couple explicit fflush() calls in my C program.
I wanted to avoid these calls and configure the output channel on
OS level to immediately output any arriving character automatically.
It seems I need something like the tcsetattr(3) 'FLUSHO', but the
man page says "FLUSHO (not in POSIX; not supported under Linux)".
What would then be the way to achieve that on Linux (if possible)?
In an application to be run on Linux I want every output character
to be flushed immediately through stdout onto the screen.
Currently I have a couple explicit fflush() calls in my C program.
I wanted to avoid these calls and configure the output channel on
OS level to immediately output any arriving character automatically.
[...]
There's one point, though, that unsettles me a bit. The setvbuf()
man page says:
"The setvbuf() function may only be used after opening a stream
and before any other operations have been performed on it."
In my code I've experimented; I made a couple initial outputs on
stdout, made a fflush() to serve my paranoia, and then switched to
unbuffered output. I also switched forth and back from unbuffered
to line-buffered output in a loop. - It *seems* to work at least.
Any experiences with the restriction documented in the man page,
or opinions and suggestions on the mode-switching I intend to do?
On 21.01.2024 17:35, Janis Papanagnou wrote:
In an application to be run on Linux I want every output character
to be flushed immediately through stdout onto the screen.
Currently I have a couple explicit fflush() calls in my C program.
I wanted to avoid these calls and configure the output channel on
OS level to immediately output any arriving character automatically.
[...]
I used a slightly modified test program based on Kenny's code
that outputs single characters with a sleep delay, both with
write(2) and setvbuf(3). I also switched buffering from mode
to mode, to see the effects.
Both suggestions in this thread work well.[*] - Thank you!
There's one point, though, that unsettles me a bit. The setvbuf()
man page says:
"The setvbuf() function may only be used after opening a stream
and before any other operations have been performed on it."
If taken literally, the consequence would be that we can use that
function just once at the beginning of a program (and never again). Especially switching modes would not be allowed or be [reliably]
possible.
On 2024-02-09, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
There's one point, though, that unsettles me a bit. The setvbuf()
man page says:
"The setvbuf() function may only be used after opening a stream
and before any other operations have been performed on it."
As of C99, the freopen function allows the path argument to be null.
You should be able to do this:
FILE *new_stream = freopen(NULL, "r", old_stream);
IMHO, using stdio for unbuffered output is even more insane than using
stdio at all (the locking model forced into it is downright
horrible). Even if you want buffered single character/ small string
output, this can be implemented on stop of write in a sane way with very little code. And the easiest to do unbuffered output is to make the corresponding system calls instead of using stdio as
proxy-system-caller.
On 09.02.2024 20:02, Rainer Weikusat wrote:
IMHO, using stdio for unbuffered output is even more insane than using
stdio at all (the locking model forced into it is downright
horrible). Even if you want buffered single character/ small string
output, this can be implemented on stop of write in a sane way with very
little code. And the easiest to do unbuffered output is to make the
corresponding system calls instead of using stdio as
proxy-system-caller.
I have to admit that I'm a bit confused about your statement since
in your initial reply you proposed both, setvbuf(3) and write(2).
This is somewhat in contrast to the strong wording "insane" you used
here. (I mean, the stdio designers must have had some idea in mind
when they invented 'setvbuf()' with '_IONBF'.)
The 'insane' is my personal opinion on the thread-safe locking model
stdio is nowadays required to follow on UNIX.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 308 |
Nodes: | 16 (2 / 14) |
Uptime: | 91:27:29 |
Calls: | 6,923 |
Calls today: | 1 |
Files: | 12,382 |
Messages: | 5,434,024 |