Re: Doubt ls-l|more
From: Sean P. Burke (sburke@dev0.welkyn.com)
Date: 04/22/03
- Next message: Charlie Gibbs: "Re: How do I set a file's date stamp?"
- Previous message: scriptOmatic: "Re: how to use wait and notify in ksh on sun system?"
- In reply to: Jens.Toerring@physik.fu-berlin.de: "Re: Doubt ls-l|more"
- Next in thread: Datha: "Re: Doubt ls-l|more"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
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
- Next message: Charlie Gibbs: "Re: How do I set a file's date stamp?"
- Previous message: scriptOmatic: "Re: how to use wait and notify in ksh on sun system?"
- In reply to: Jens.Toerring@physik.fu-berlin.de: "Re: Doubt ls-l|more"
- Next in thread: Datha: "Re: Doubt ls-l|more"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] [ attachment ]
Relevant Pages
|
|