[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [PATCH] luaconf.h: call fflush(NULL) before popen()
- From: Alexey Tourbin <at@...>
- Date: Sat, 8 Sep 2007 21:55:05 +0400
On Mon, Aug 27, 2007 at 08:40:35PM +0200, J?rgen H?tzel wrote:
> > And at least some scripting languages call something like "fflush(NULL)"
> > before spawning (see e.g. "perldoc -f system").
> >
> > (And also "fflush(NULL)" is K&R-compatible.)
>
> I wouldn't enforce this... If you want to disable output buffering, you can already do this.
>
> io.stdout:write("From Lua...")
> io.popen("echo -n From Pipe...", "w")
> io.stdout:write("Again from Lua...")
>
> bash-3.2$ lua unflushed.lua >unflushed.txt; cat unflushed.txt
> >From Pipe...From Lua...Again from Lua...
You cannot *rely* on this behaviour (that "From Pipe..." comes first)
anyway. Buffers are flushed at more or less random points, and
so there seems to be no single problem with calling fflush(NULL)
at less random points.
> Now flush stdout before calling popen:
>
> io.stdout:write("From Lua...")
> io.stdout:flush()
> io.popen("echo -n From Pipe...", "w")
> io.stdout:write("Again from Lua...")
>
> bash-3.2$ lua flushed.lua >flushed.txt; cat flushed.txt
> >From Lua...From Pipe...Again from Lua...
Sure you can call flush() yourself so as to make flushed
and unflushed output the same. You can even check (maybe not
in lua) whether the output is buffered. But there's no good
reason to assmue, from the design of high-level language,
that those outputs should ever be different, depending on
peculiarities of that "stdio" thing.
I.e. I can understand why fork(2) does not call fflush(3)
automatically. This is because fork() is a system call
and it should not be aware of something like fflush() call
from stdio library. But in higher-level languages, like perl,
this seems to be more consistent. And this is what perl does.
Attachment:
pgpBYiELsgtAs.pgp
Description: PGP signature