From: Max Ruttenberg <mruttenberg@emutechnology.com>
To: musl@lists.openwall.com
Subject: Re: Enforcing expected ordering of operations on stdout, stdin, and stderr
Date: Wed, 10 Feb 2016 18:08:47 -0500 [thread overview]
Message-ID: <CAD+Cw=cVbgEUz_VETZL9-rOP+aaDxGqW0CGAbKYi+cbpq9GwhA@mail.gmail.com> (raw)
In-Reply-To: <1455141901.26335.95.camel@zhasha.com>
[-- Attachment #1: Type: text/plain, Size: 2275 bytes --]
>
> fflush(stdout);
> This is more of a basic C thing than a libc ml thing. You should
> consider picking up a copy of The C Programming Language by Kernighan
> and Ritchie. It will explain all of this.
I know about fflush, thanks.
Consider this program:
int main()
{
char buff[2];
puts("enter a character");
buff[0] = getchar();
buff[1] = '\0';
puts(buff);
return 0;
}
If I compile that on linux-amd64, with or without musl, I will see "enter a
character" printed to my console and then be prompted for a character, as
opposed to the other way around. I don't know if this is formally
guaranteed by the C standard, but somehow that order seems to be
maintained.
But if I grep the source code in musl/src/stdio for "fflush" I don't see a
bunch of calls to fflush. I see a call to it in fclose and freopen... but
that's neither surprising nor helpful. If I do the same in
musl/src/internal I also don't get anything. I've even tried just greping
for "flush."
And yet somehow the order is maintained within those calls to puts and
getchar. So what I'm asking is: how? What part of the internal musl source
even attempts to enforce that ordering? I know the calling application can
do it with calls to fflush, but somehow that doesn't seem to be necessary
short of a signal interrupting the expected flow of execution. Am I just
getting lucky 100% of the time or is there some source in the stdio library
that's enforcing this?
On Wed, Feb 10, 2016 at 5:05 PM, Joakim Sindholt <opensource@zhasha.com>
wrote:
> On Wed, 2016-02-10 at 16:49 -0500, Max Ruttenberg wrote:
> > All,
> >
> >
> > I guess my question is more easily asked through an example. If I have
> > code that makes a call to puts and then a call to getchar, what
> > mechanism enforces that stdout gets flushed before blocking for stdin?
> > Is there a such a mechanism? My gut says yes but I haven't been able
> > to pinpoint it.
> >
> >
> > Thanks,
> > Max
>
> fflush(stdout);
>
> This is more of a basic C thing than a libc ml thing. You should
> consider picking up a copy of The C Programming Language by Kernighan
> and Ritchie. It will explain all of this.
>
>
--
Max Ruttenberg,
Member of the Technical Staff
Emu *Technology*
1400 E Angela Blvd, Unit 101
South Bend, IN 46617
[-- Attachment #2: Type: text/html, Size: 3963 bytes --]
next prev parent reply other threads:[~2016-02-10 23:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-10 21:49 Max Ruttenberg
2016-02-10 22:05 ` Joakim Sindholt
2016-02-10 23:08 ` Max Ruttenberg [this message]
2016-02-10 23:22 ` Szabolcs Nagy
2016-02-10 23:39 ` Rich Felker
2016-02-11 1:17 ` Max Ruttenberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAD+Cw=cVbgEUz_VETZL9-rOP+aaDxGqW0CGAbKYi+cbpq9GwhA@mail.gmail.com' \
--to=mruttenberg@emutechnology.com \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).