Unix pipelineing

Hi Guys?

How is I/O between processes handled in a Unix pipeline and what kind of advantages would it offer to a system?

Best Regards

Who is Participating?
brettmjohnsonConnect With a Mentor Commented:
Unix pipelines are a chain of processes that pass from one end
of the chain to the other.

Each Unix process inherits 2 already opened File descriptors:
   stdin - for reading input from previous program in pipeline
   stdout - for writing output to the next program in the pipeline

The pipeline is constructed by linking the stdout of one pipe segment
to stdin of the next pipe segment.  For instance:
    procA | procB | procC
Whatever procA writes to its stdout File descriptor, procB can then
read from its stdin File descriptor.  And whatever procB writes to its
stdout, procC can then read from stdin.

You can see where this is going.  Each process in the pipeline can
read some data on its input, massage that data in some way, then
write it out.  One of the Unix programming philosophies is:
  "Each program should do one thing and do it well."
And another is
  "Many small programs, each performing a simple task can be combined
to perform a complex task."

Consider the following moderately complex pipeline:
   echo "Total Folders: " `ls -aRF1 $1 | egrep -v '^\.{1,2}/$' | egrep '.*/$' | wc -l`

The command pipeline is everything between the backquotes (`) and consists of
four separate segments:
1)  ls -aRF1 $1 |   performs a recursive directory listing starting from the specified directory, with options that dictate output format
2) egrep -v ...   |  take the list of filenames, removing all entries for '.' (current directory) and '..' (parent directory)
3) egrep '.*/$'  |  take the list of filenames, only passing on those that end in '/' (only directorys)
4) wc -l               count the number of lines in the resulting list (ls -1 produces on entry per line)

The result is the number of sub-directories in the directory tree specified.
That output is appended to the line "Total Folders: " and printed out.

sounds like a homework, could you please be a bit more specific
gopalpatelAuthor Commented:
Brett thanks mate.

Best Regards

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.