Using tee to create a log

Kevin Buettner plug-discuss@lists.PLUG.phoenix.az.us
Thu, 26 Jul 2001 15:05:30 -0700


On Jul 26,  2:05pm, Alan Dayley wrote:

> How does the piped program know that it is piped?  I would think that all
> it knows is that it is outputing to sdtout.  Who changes the data in the
> pipe?  The shell?

The shell is responsible for parsing your shell commands which may
contain redirection operators, pipe symbols, and other constructs.  It
then invokes a sequence of system calls to "run" the command under
consideration.  It does this by using open() [for opening files], dup2()
[for creating some of the redirections], pipe() [for creating pipes],
fork() [for creating new child processes], and one of the exec() functions
[for executing a program].   (Actually, none of the exec() functions
are named exec.  If you're looking for a man page, see execl() or
execv().)

Thus when you say something like

    cmd1 | cmd2,

The shell will fork and exec two processes and create a pipe which
is used to connect stdout of cmd1 to stdin of cmd2.

Either cmd1 or cmd2, may use fstat() to obtain information about
stdout or stdin (or any of its file descriptors).  The st_mode member
in struct stat may be inspected to determine whether the descriptor in
question is connected to a pipe or not.  For more information, see the
stat(2) man page.

Kevin