I am creating a program that can support piping between any number of processes. For 2 processes, you create 1 pipe, and use it between them.
However, for 3 or more pipes, this method will not work because of writing to a pipe while you still need the old one. Example:
If you write to pipe again (the red part), it will overwrite the previously written information, won't it?
What is the proper way to do this?
I have found that you can just create 1000 pipes or something like in the example below:
However, since I need to be able to support ANY number of pipes, and not just 2 or 3, I do not want to do this.Code:http://www.cs.loyola.edu/~jglenn/702/S2005/Examples/dup2.html int pipes; pipe(pipes); // sets up 1st pipe pipe(pipes + 2); // sets up 2nd pipe // we now have 4 fds: // pipes = read end of cat->grep pipe (read by grep) // pipes = write end of cat->grep pipe (written by cat) // pipes = read end of grep->cut pipe (read by cut) // pipes = write end of grep->cut pipe (written by grep)
Does anyone know how this can be achieved properly?
Can I do something like this?
Anyone have any insight?Code:int old_write_pipe = mypipes; // save the old write pipe int new_write_pipe; // new write pipe mypipes = new_write_pipe; // replace the old write pipe in the array with a new file descriptor pipe(mypipes);