pipe/redirect dumb question
Bob Bell
bobbell at zk3.dec.com
Wed Dec 11 23:01:29 EST 2002
On Wed, Dec 11, 2002 at 10:41:52PM -0500, Erik Price <erikprice at mac.com> wrote:
> Does the code that tells the reader or writer to block have to be
> written into the application, or is that somehow built into the shell?
> If it needs to be built into the application, well then say for
> instance I'm writing a Perl script. I've never programmed my Perl
> script to block if the writer gets "full". So perhaps it's handled
> internally by Perl. But if I'm writing a C program, I shouldn't make
> the assumption that this will be done for me.
>
> Unless of course I'm totally off the mark here, and the rules of piping
> are handled by the shell or OS or something.
Applications don't normally do their own flow control. When you use
'|' in the shell, basically all the shell does is create a pipe (see
`man 2 pipe`), and connect the write end to the stdout (fd 1) of the
first app, and the read end to the stdin (fd 0) of the second app. When
you use redirection, like '<', the shell opens the file for reading, and
dups (see `man 2 dup2`, which also applies in the previous case) the fd
for the file to the program's stdin (again, fd 0). The programs just
read from stdin and write to stdout as 'normal', but the file
descriptors are not what is normally the case without piping or
redirection.
As far as blocking goes, that just happening when a write buffer is
full or there is no data in the buffer (yet) for a read. That buffer
could potentially be in libc and/or the kernel, depending on your
precise semantics. This is true for whatever programming language you
are using, unless of course you have specified non-blocking behavior.
With '<' shell redirection, you are reading directly from a file, so you
wouldn't expect read() to normally take very long to complete, though it
may need to block to bring the data into the kernel cache. When
read()ing from the output of another program, as is the case with '|',
you may find yourself blocking more, depending on the producer's ability
to keep up with the consumer.
--
Bob Bell <bobbell at zk3.dec.com>
-------------------------------------------------------------------------
"Every rectal thermometer made by Q-Tip has been personally tested."
-- Material packaged with a Q-Tip rectal thermometer
More information about the gnhlug-discuss
mailing list