I am programming a shell in c++. It needs to be able to pipe the output from one thing to another. For example, in linux, you can pipe a textfile to more by doing cat tex
For a simple, two-command pipeline, the function interface you propose may be sufficient. For the general case of an N-stage pipeline, I don't think it is flexible enough.
The pipe()
system call is used to create a pipe. In context, you will be creating one pipe before forking. One of the two processes will arrange for the write end of the pipe to become its standard output (probably using dup2()
), and will then close both of the file descriptors originally returned by pipe()
. It will then execute the command that writes to the pipe (cat textfile
in your example). The other process will arrange for the read enc of the pipe to become its standard input (probably using dup2()
again), and will then close both of the file descriptor originally returned by pipe()
. It will then execute the command that reads from the pipe (more
in your example).
Of course, there will be still a third process around - the parent shell process - which forked off a child to run the entire pipeline. You might decide you want to refine the mechanisms a bit if you want to track the statuses of each process in the pipeline; the process organization is then a bit different.