Re: Doubt ls-l|more

From: Sean P. Burke (sburke@dev0.welkyn.com)
Date: 04/22/03


From: sburke@dev0.welkyn.com (Sean P. Burke)
Date: Tue, 22 Apr 2003 19:02:39 GMT


Jens.Toerring@physik.fu-berlin.de writes:

> Datha <dathak@semsoftindia-samsung.com> wrote:
> > I have one new doubt of Unix.
> > We have the combination of commands called ls -l|more.Can somebody
> > explain working of this comand. My doubt is, let me list down the
> > probable steps.
> > 1. Shell will fork one child to execute abve command.
>
> No, the shell will fork *two* processes for the above command,
> one running 'ls' and another one for running 'more'.
>
> > 2. Ls executes which will list the directory contents.
> > 3. LS actually has to feed the data into a pipe so that more can use
> > it.
>
> No, it doesn't. The process running 'ls' just writes to its
> standard output without needing to know where its really
> writing to. The trick is that the shell starts the process,
> which then executes 'ls', with its standard output redirected
> to a pipe. So, the shell basically does the following:
>
> shell: create pipe
> shell: fork
>
> After the fork you now have two processes. The parent process
> is the shell, while in the child process now it happens:
>
> child 1: redirect standard output to the pipe
> child 1: exec() 'ls -l'
>
> exec() is one of a family of functions that replaces the code of
> the shell with the 'ls' program. So 'ls' gets run, automatically
> sending its output to the pipe. The function for redirecting
> standard output to the pipe is dup(2) or dup2(2).
>
> > 4. more will read from the pipe and display on Screen.
>
> For 'more' the shell starts another process, this time with its
> standard input redirected to the same pipe the process running
> 'ls' is writing to:
>
> shell: fork
>
> child 2: redirect standard input to the pipe
> child 2: exec() 'more'
>
> So now whatever 'ls' writes goes to the pipe, and whatever 'more'
> reads comes from the pipe.
>
> > 5. That looks like end of story.
>
> Mostly. There's some additional tickery in that 'ls' is able to
> figure out from within itself if it's writing to a terminal or
> to a file or pipe and may change its output format accordingly
> (e.g. by printing out one file per line instead of several beside
> each other etc.).
> Regards, Jens
> --
> _ _____ _____
> | ||_ _||_ _| Jens.Toerring@physik.fu-berlin.de
> _ | | | | | |
> | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
> \___/ens|_|homs|_|oerring

I would only add that the 'redirect' part is accomplished
by using close() and dup2() (which may not be obvious).
Look at the source for popen() in Linux or BSD to see
how it's done.

-SEan



Relevant Pages

  • Re: Doubt ls-l|more
    ... Shell will fork one child to execute abve command. ... LS actually has to feed the data into a pipe so that more can use ... with its standard output redirected ...
    (comp.unix.questions)
  • Re: How many pipes in this complex shell command?
    ... I'm wondering how a shell would run the following ... > Redirect stdout of ps to write end of pipe A ... > Redirect stdout of grep to write end of pipe B ...
    (comp.unix.programmer)
  • Re: Why wont this system command run?
    ... You can't do pipe or redirect in the Runtime.execargument. ... >> to execute the commands within a shell like bash, csh, ksh, etc. ... > of the above string in double quotes. ...
    (comp.lang.java)
  • Re: Advice on a Dunhill purchase
    ... Take a look at the Shell line or the County line - both are very agressively ... As to taking the plunge and trying your first Dunhill ... ... - mostly billiards because of my preference in pipe shapes. ... They all smoke flawlessly. ...
    (alt.smokers.pipes)
  • Re: Backgrounding a stream, and sendmail?
    ... >> just redirect the output to a file instead of sendmail.) ... > shell command like ... when prog_b has it's stdout redirected nothing ... > is done instead of printing to stdout you could start sendmail from within ...
    (comp.os.linux.development.apps)