Google Answers Logo
View Question
 
Q: operating system (unix) ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: operating system (unix)
Category: Computers > Operating Systems
Asked by: vpb-ga
List Price: $5.00
Posted: 18 Dec 2002 11:00 PST
Expires: 17 Jan 2003 11:00 PST
Question ID: 126536
How would you expect a shell to implement the pipeline below?
                                 ls -ls | grep user | sort

Request for Question Clarification by krobert-ga on 18 Dec 2002 11:22 PST
Are you asking what this string does?

krobert-ga
Answer  
Subject: Re: operating system (unix)
Answered By: maniac-ga on 18 Dec 2002 16:22 PST
Rated:4 out of 5 stars
 
Hello Vpb,

The short answer is that the shell will...
 - create the pipe(s) in your example - two of them
 - fork a child to run the second component (and third); adjust stdout
and stdin of each child to be connected in series through the pipe
 - exec each component
Using your example, there are three processes connected by pipes as
follows
  ls -ls (std out directed to next process std in)
  grep user (std out directed to next process std in)
  sort (std out to the output of the shell)
to produce a list of files, grep'd to just the names /w "user" in
them, and then sorted.

There are several on line references with this kind of information.
One of the best I found was titled "Putting it all together: Unix
Program Execution" at
  http://www.isi.edu/~govindan/cs402/lectures/unixprogram.html
which has some pseudo code that attempts to describe the top level
process of executing a command.

More detail on pipes, fork, exec, and popen is at
  http://cs.senecac.on.ca/~lczegel/UnixProg/Lecture11.html
which also describes redirection in some detail.

Some good illustrations of pipes and processes at
  http://www.cis.ohio-state.edu/~mamrak/CIS762/pipes_lab_notes.html
which at the end includes a reference to a minimal Bourne style shell
(ash) if you want to review the real source code. It also has some
nice illustrations of the steps described in the first reference.

A powerpoint presentation that has some good illustrations and source
code at
  http://flinflon.brandonu.ca/Dueck/2001/62306/Processes/unix.ppt

For additional references, try searching with
  unix shell pipe fork exec

Let me know if you need some additional clarification.

  --Maniac

Request for Answer Clarification by vpb-ga on 18 Dec 2002 17:19 PST
hi Maniac


i am happy but not that much  i want bit more explanation please


(that means easy to understand )

Clarification of Answer by maniac-ga on 19 Dec 2002 18:47 PST
Hello Vpb,

... more explanation ... easy to understand. Hmm. I don't know if I
can provide an easier to understand answer but I will describe what
happens in a different manner to see if that works for you.

The Unix shell reads a line of text and interprets it according to a
set of rules. In the example you provided, there are three commands as
follows.
  ls -ls
  grep user
  sort

The first "argument" (or word) of each command refers to a program
that will be run. For example "ls" is a program that produces a list
of files that varies based on the arguments that follows. If you enter
  man ls
to a Unix system you will get an explanation of the ls command which
on my system say that "-l" means to provide a "long format" listing of
the files and -s means to display the total number of 512 file system
blocks for each file (in addition to the long format output). You can
do the same for each of the other commands.

Each of the programs listed above will run in a separate process.
Generally that is done using fork and exec calls as described in the
original answer. In this case, it is necessary to connect the output
from the first program to the input to the second, and the output from
the second program to the input to the third. That is what the pipe
(|) symbol represents. This is done by the shell using the pipe
function call and some manipulation of the file descriptors by the
shell.

Each process will run, in parallel, to completion with the input and
output connected as I described it. When they are finished, the shell
ensures the new processes, pipes, and other resources are cleaned up.

For more description of the specific system calls and functions I
suggest the following Unix commands
  man fork
  man exec
  man pipe
to understand what each does,
  man ls
  man grep
  man sort
to understand what the individual commands do, and
  man sh
to provide a user level description of the shell program.

  --Maniac
vpb-ga rated this answer:4 out of 5 stars

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy