This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: glibc calls more write() than necessary
Jan Kratochvil <jan.kratochvil@redhat.com> writes:
> On Sun, 21 Jan 2007 22:34:35 +0100, Matthieu Lemerre wrote:
>> Mike Frysinger <vapier@gentoo.org> writes:
> ...
>> > line buffered means line buffered ... it doesnt mean buffer some lines but not
>> > others
>> > -mike
>>
>> OK then, if the standard precisely defines that flushes must be done
>> at all lines. I don't have it here, so I can't tell.
>
> http://www.opengroup.org/onlinepubs/009695399/functions/setvbuf.html
> ->
> http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_05.html#tag_02_05
> When a stream is ``line buffered'', bytes are intended to be
> transmitted as a block when a newline byte is encountered.
>
> Not written as "every" but it is probably the only right interpretation.
>
>> Note however, that you get the same observed behaviour when you
>> flushes only the last '\n'.
>
> Reader of a pipe will see the difference, it may cause regressions.
I first agreed to this, but I then tried the following test case:
int
main (int argc, char **argv)
{
printf ("\n"); /* Does not work when there is only one printf. */
printf ("\nhello\n\n\n\n\n\n\n\nee\nee\n\n\n\nee\n\n\n\n\n\n");
}
yields
write(1, "\n", 1
) = 1
write(1, "\nhello\n\n\n\n\n\n\n\nee\nee\n\n\n\nee\n\n\n\n\n", 30
hello
ee
ee
ee
) = 30
write(1, "\n", 1
) = 1
So flushes are already sometimes done together.
Note: The second printf compacts the write only if there was already a
non-empty printf before. I guess it depends on whether a buffer has
already been allocated or not.