Hi
I've made a program that create 5 pipes and fork 5 processes in a loop. I've managed to send data from the parent to each child processe, when each child processe is overlapped by another program. Each loop is done in the following manner
(parent.c):
// Child process - reads from pipe
if (childpid == 0) {
dup2(fd[0], 0); // replace stdin with pipe input
execv("program", arguments);
} else { // Parent process - writes to pipe
write(fd[1], buffer, strlen(buffer)+1);
}
So now i'm able to get the data that is sent from the parent to the pipe, by reading from STDIN_FILENO in the program the childs executes with execv(...).
Like this (program.c):
char *buffer = (char *)malloc(50);
read(STDIN_FILENO, buffer, 50);
My problem however is, how can I send data back to the parent? I was thinking of something like replacing stdout with the pipe output by using dup2 again, but I can't get it to work. I realise this have to be done before using execv(...) at least.
I'm not sure if that explanation was sufficient so I can make a little image with text : )
This is how it is right now:
- Parent -> Pipe
- Pipe -> Child process 1
- Pipe -> Child process 2
- Pipe -> ...
- Pipe -> Child process 5
I want it to be like this.
- Parent -> Pipe
- Pipe -> Child process 1
- Pipe -> Child process 2
- Pipe -> ...
- Pipe -> Child process 5
- Child process 1 -> Parent
- Child process 2 -> Parent
- ...
- Child process 5 -> Parent
Thankful for help!