I'm practicing piping two processes together. I want to have both processes communicating with each other using two pipes. Here's what I have so far:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
int main(){
//setting up two pipes
int pipe1[2];
int pipe2[2];
pipe(pipe1);
pipe(pipe2);
//setting up child and parent
pid_t pid = fork();
if(pid < 0){
perror("fork failed\n");
exit(-1);
}
if(pid == 0){
printf("child\n");
dup2(pipe1[1], STDOUT_FILENO);
close(pipe1[1]);
close(pipe1[0]);
dup2(pipe2[0], STDIN_FILENO);
close(pipe2[0]);
close(pipe2[1]);
}
if(pid > 0){
wait(NULL);
printf("parent\n");
dup2(pipe1[0], STDIN_FILENO);
close(pipe1[0]);
close(pipe2[1]);
dup2(pipe2[1], STDOUT_FILENO);
close(pipe2[0]);
close(pipe2[1]);
}
return 0;
}
I would like both processes communicate with each other by having the input of one process be the output of the other. Have I set this up correctly? If so, where could I go from here if for example I would like the parent to send integers to child and the child were to perform operations on them, and then send them back to the parent for printing.
Thanks.