How do I chain stdout in one child process to stdin in another child in C? -
I'm moving around in C, trying to figure out how to do this. Let's say that my main program is the original process. Parents prepare the processes of three children, each of which will eventually run (but it is not important anymore). What I would like to do, it is to do that the first child's standout will be obtained by the other child's studin. The second child's standout will then be received by the third child's studin. The stdin / stdout of the original process does not mess at all.
So far, what did I get?
pipe (procpipe); ParentPid = getpid (); (I = 0; i But from there, I've been stuck about how to use dup2 to allocate my pipe properly, and insert it into that section of my code There are so many examples on Google to do and how this is a pipe website from a parent to a child, but I still do not see anybody who will tell me how to get a child's standout from another child's studin Be connected .
Edit: forgot to mention: Suppose all my variables started correctly. Int 'mynumber' is such a baby process that knows about the universe which is this number, so I can give it instructions through
if any (mynumber == whatever )
Then you have a loop that creates many child processes. Each of these hair processes is using two pipes: Read from the back and write on the next To set a pipe for the reading end, you need to close the end of the pipe type , And the end of reading dup2 in stdin Similar to the pipes, where the process will be writing. zero set_ad (int * lpip) {a dp2 (lpepe [0], STDINIFLNO); One by one one (lpipe [0]); // We already have a copy, then turn it off (LPI [1]); // do not use this closing) a zero set_write (int * rapippe) {a DP2 (RPEPEP [1], STDOUTIFELNO); One ace one (rpipe [0]); // do not use this closing (approx [rpipe [1]); // We already have a copy, then turn it off} When you give each child a thorn, you need to attach it to the pipe. Void fork_and_chain int * lpipe, int * rpipe (if (fork!) (A) one (a) one (if lpipe) // There is a pipe from the previous process one a one One   set_read (lpipe); // Otherwise you may want to redirect input from anywhere for startup (rppe) // if there is a pipe for the next process, then a set_lit (rpipe); / / and then you Lastly / want to redirect your luggage to elsewhere and make sure that the hair process is here / / So this code does not continue to run the code.
In this hand you can now write a loop which constantly connects the forks, pipe, and then Of course, once the two ends of the pipe have been linked to the child processes, the parents should not leave it for themselves. < Pre> // It is believed that at least two Actions can be chained:) // two pipes in series, a series int lpipe the past [2], rpipe [2]; // First create the Output Pipe Pipe (RPEP); // The first child takes input from fork_and_chain (NULL, rpipe) elsewhere; // Output pipe gets input for next process LPi [0] = RPP [0]; LPiP [1] = RPP [1]; // series for all but first and last children (i = 1; i Closing bits are very important! When you give fork with an open pipe, then there will be four open file descriptors: two on the basic process and on the process of two other children. You have to close all those that you will not use. This is the reason that the above code always closes the irrelevant edge of the pipes in hair processes, and both ends on the parent.
Also keep in mind that I am giving special treatment to the first and final processes, because I do not know where the input from the series will come from, and where the output will come.
Comments
Post a Comment