How pipe works in this command

when we give piped commands to the shell
e.g. ps -aef |grep httpd |grep -v grep

What happens internally, Does shell really uses pipes or named pipes (fifo),if yes then how  ??
Who is Participating?
chris_calabreseConnect With a Mentor Commented:
jools has it basically right, but the processes are not spawned serially but simultaneously.

Internally, the shell calls:
  o pipe() to create the pipe channels inside the kernel
  o fork() to create multiple child-processes (one for each command in the pipeline plus the original "parent" shell)
  o close() and dup2() to redo the plumbing of the child-processes so their stdin and stdout's are connected appropriately.
  o execve() (or one of it's cousins) so the child-processes can run the actual commands they're supposed to.
  o wait() so the parent process can wait for the children to exit().
I hope this makes's been a while since I did this...

Firstly, pipes are pipes (Eh!), not fifo's

The command in you example will run the ps command and then (I think) spawn another process using the stdout from ps to stdin of the grep this passes the process listing to grep.

grep then extracts all occurances of httpd and in turn passes this to stdout so when the last grep is spawnd this is used as it's input, the grep -v grep removes your actual command and just returns the httpd process line to stdout.

fifo's are normally used in print q's and such like.

I hope this goes somewhat to answer your question, if you want more detail, please post again and I'll drag out some old notes from 1985 ish if I can find them!

Sorry, forgot to check mail response!!!
Thanks chris, I did say it'd been a while, glad to know I havn't lost touch *that* much.

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects that and the expert will now receive these points, all verified.

Please click on your Member Profile, select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.
Moderator @ Experts Exchange
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.