How does buffering actually work?

sarn sarn at theartofmachinery.com
Thu Feb 28 22:17:34 UTC 2019


On Thursday, 28 February 2019 at 21:17:23 UTC, Cleverson Casarin 
Uliana wrote:
> It works almost perfectly, except that it doesn't wait for my 
> first Enter after printing "First name: value1". Rather, it 
> prints both "First name: value1" and "First name: value2" 
> together on the same line, then it starts to behave as 
> expected, e.g. printing one line at a time and waiting for me 
> to press Enter.

Perhaps that happened with some other variation of the code.  The 
code you wrote shouldn't work like that (it doesn't for me when I 
tried, at least).

Ali has some good answers for fixing your code.  (readf("\n") 
also works, BTW.)  Hopefully this helps with the "How does 
buffering actually work?" question:

D uses the system's standard C library for IO, like most 
programming languages do, so IO buffering isn't fundamentally 
different (but some high-level functions might have different 
behaviour).

The standard C library provides buffered IO for input and output. 
  By default terminal IO is line-buffered (not sure if that's all 
systems), so you might see delays up until a newline, but 
line-by-line IO won't notice the buffering.

What happens here?

write()
read()
write()
read()

The first write goes to the output buffer.  If the buffer ever 
gets full (or has a newline in the case of line buffering), the 
data gets flushed to the real output.

At the read, it's possible there's still some data in the output 
buffer that's not flushed.  If needed, you can explicitly call 
flush() to make sure there isn't.  If there happens to already be 
data in the read buffer, read() will take as much as it needs to. 
  If there isn't enough, then real input will happen, and the call 
will block until data comes in.  The real read will ask for a 
chunk of data, which will often be more than the read() call 
needs.  The remainder gets put into the buffer (that's what it's 
for).  (The kernel and libc actually both have IO buffers.)

In any case, the second write won't happen until the read has 
finished.

Rinse and repeat for the remaining lines.


More information about the Digitalmars-d-learn mailing list