In a comment I asked
If you were to invoke ./create_bytes.py -n 100000000 | ./my_prog
, how would you expect it to work?
and you replied
It should print out 100000000 I guess
So let's think about this, and ask: How could this possibly work?
The create_bytes.py
script is going to write 100,000,000 bytes. Where do they go? Into the pipe.
But what happens over in my_prog
? It doesn't actually read any characters from the pipe, it just asks, what is the "size" of the pipe?
But if create_bytes.py
has written 100,000,000 characters, and if my_prog
hasn't read them, where are they? Are they all "in the pipe"? And the answer is, no, they are not.
Pipes have a finite capacity. If they fill up, and if the reader doesn't read characters out fast enough, the operating system automatically puts the writing process to sleep. The writing process isn't woken up again, isn't given the opportunity to write any more characters, until some empty space has cleared up in the pipe for it to write into again.
My point is that if pipes have a finite capacity (as I assert that they do), it's impossible for the example I posed to print "100000000", for the simple reason that there is no piece of code, anywhere, that can possibly read and count those characters.
You might imagine that fstat
ought to read and count them in this situation somehow, but (a) it doesn't and (b) it couldn't. If fstat
read characters from the pipe so it could count them, the characters would be gone. If your program then tried to read them (perhaps down below the ...
you had in your code fragment), it wouldn't be able to read them, and that would be Wrong.
But, to convince yourself, I encourage you to try that invocation
./create_bytes.py -n 100000000 | ./my_prog
and see what you get. I'll bet you $100 you don't get "100000000", but the result you do get might be interesting.
I don't have your create_bytes.py
script, so instead I tried
yes | a.out
yes
is a standard Unix program that prints "y" an infinite number of times. a.out
was where I'd just compiled your test program, after fixing it up a bit. And, on my machine, it printed
65536
So evidently, on my machine, when fstat
is called on a file descriptor that's connected to a pipe, fstat
fills in st_size
with the size of the contents of the pipe, and on my machine, pipes evidently have a capacity of 65536, which is of course 216.