How to make a pipe in c++

node ninja picture node ninja · Apr 30, 2011 · Viewed 14.2k times · Source

I'm looking at the code for a c++ program which pipes the contents of a file to more. I don't quite understand it, so I was wondering if someone could write pseudocode for a c++ program that pipes something to something else? Why is it necessary to use fork?

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Apr 30, 2011
create pipe
fork process
if child:
  connect pipe to stdin
  exec more
write to pipe

You need fork() so that you can replace stdin of the child before calling, and so that you don't wait for the process before continuing.