Multiple forked processes and pipes

I am new to C++ and, in the interest of disclosure, this is related to a programming assignment for a class. I am not going to ask any questions that could be construed as having anyone do the work for me.

The program requirements are to mimic the Linux shell in the execution of the ps -A | grep argv[1] | wc -l command.

The program requires 3 forks, with the execution as follows:

great grandchild: ps -A
grandchild: grep argv[1]
child: wc -l
parent: display output

I have code that presently creates pipes, forks the required number of times, but the only output from the program is the initial ps command. I get an error referencing a bad file descriptor for the child process' wc command.

Can one pipe be used to handle communications between all the processes? Or should I have 3 pipes (pipe1 parent-child, pipe2 child-grandchild, etc)? If multiple pipes, do they all need to be established in the parent process?
This one is what you need:
Or should I have 3 pipes (pipe1 parent-child, pipe2 child-grandchild, etc)?
Topic archived. No new replies allowed.