The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
From: lm@mcvoy.com (Larry McVoy)
Subject: [TUHS] the sin of buffering [offshoot of excise process from a pipeline]
Date: Mon, 14 Jul 2014 19:40:42 -0700	[thread overview]
Message-ID: <20140715024042.GD13698@mcvoy.com> (raw)
In-Reply-To: <201407150231.s6F2VRK0022875@coolidge.cs.dartmouth.edu>

On Mon, Jul 14, 2014 at 10:31:27PM -0400, Doug McIlroy wrote:
> > Err, why is buffering data in the process a sin? (Or was this just a
> humourous aside?)
> 
> Process A spawns process B, which reads stdin with buffering. B gets
> all it deserves from stdin and exits. What's left in the buffer,
> intehded for A, is lost. Sinful.

It really depends on what you want.  That buffering is a big win for
some use cases.  Even on today's processors reading a byte at a time via
read(2) is costly.  Like 5000x more costly on the laptop I'm typing on:

calvin:~/tmp lmdd opat=1 move=100m of=XXX
104.8576 MB in 0.1093 secs, 959.5578 MB/sec
calvin:~/tmp time a.out fd < XXX

real    0m14.754s
user    0m1.516s
sys     0m13.201s
calvin:~/tmp time a.out stdio < XXX

real    0m0.003s
user    0m0.000s
sys     0m0.000s
calvin:~/tmp bc
14.754/.003
4918.00000000000000000000

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define unless(x)       if (!(x))
#define streq(a, b)     !strcmp(a, b)

main(int ac, char **av)
{
        char    c;

        unless (ac == 2) exit(1);
        if (streq(av[1], "stdio")) {
                while ((c = fgetc(stdin)) != EOF)
                        ;
        } else {
                while (read(0, &c, 1) == 1)
                        ;
        }
        exit(0);
}




  reply	other threads:[~2014-07-15  2:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-15  2:31 Doug McIlroy
2014-07-15  2:40 ` Larry McVoy [this message]
2014-07-15 18:55 ` scj
2014-07-15 23:43 Doug McIlroy
2014-07-16  0:32 ` Larry McVoy
2014-07-16  3:53   ` John Cowan
2014-07-16  4:05     ` Larry McVoy
2014-07-16  6:03       ` John Cowan
2014-07-16 14:30         ` Larry McVoy
2014-07-16 14:56           ` Dan Cross
2014-07-16 15:41             ` Larry McVoy
2014-07-16 21:46 Noel Chiappa

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=20140715024042.GD13698@mcvoy.com \
    --to=lm@mcvoy.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.
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).