Oh I see, that makes sense now. So the reason this is hard to do with a single pipe is that as soon as you reuse that pipe for a second command, it's going to start writing data into the same pipe it's trying to read from - and it's just going to end up reading in it's own output once the intended data is out. If you were to do this with a single pipe your program would need to read everything from the output pipe in to a memory buffer, and then re-write it to the next input pipe before the next program could run.

So your approach is to use 2 pipes, so that instead of having to read, buffer, and write it yourself, each alternating program will do that for you, leaving the previous pipe available and empty for the next program to use. That's good - seems like it would work. I see a couple of problems with your implementation though. As a general recommendation, I still think this might be cleaner for you to implement this iteratively, using a loop instead of a recursive function. I'd suggest you see if you can write a simple example that just pipes data between two hard-coded commands, then three, and then scale up to a loop once you get that working.


  • You're creating new pipes every time you call processLine(), so technically you still aren't complying with what your teacher wants - you'll create more than 2 pipes. You can ensure and prove that you never use more than 2 pipes by creating your pipes ahead of time, and then just passing them around to the commands that need them.



  • It looks like you're overwriting one end of the pipe with stdin for your first command, and trying to use dup to use stdout for your last command. What you actually want to do is leave the results of pipe() alone and use each pair of fds between consecutive commands. Does that make sense?