I’ve seen this question before, but still a bit confused: how would I create communication between child processes of the same parent? All I’m trying to do at the moment is passing a message from the first child process to the nth child process. My idea is to create n-1 pipes in the parent process and then redirect the parent’s ends to the next child process. What I can’t figure out is, how would we redirect the ends from the parent if the next child process hasn’t been created? I feel there’s an issue in the way I’m approaching this.
EDIT: My goal is to print the message that was passed from the first child process to the last one. It’s a simple program.
You don’t need to create the processes first. The solution is as follows: you first create all the needed pipes, save them in some array. Then you do a
fork, redirect input and output streams (for the child process) accordingly, close unused pipe ends and perform anexec. Pipes can exist without the corresponding processes, they have buffers, so you can write to a pipe while nobody’s still reading, it will be ok.You should just be aware of closing unused id’s before doing
exec. And be careful writing to a pipe which input endpoints (all the input endpoints) could become closed: it could result in aSIGPIPE.