I am trying to write this little program, where the parent and the child communicate with each other via pipes, the code here works, unless you 'uncomment' the commented lines, than it comes to some sort of deadlock, and I cannot figure it out why? Any ideas?
int main(int argc, char **argv){
int fd[2];
int fd2[2];
pid_t pid;
pipe(fd);
pipe(fd2);
pid = fork();
if(pid==0){
close(fd[1]);
dup2(fd[0],fileno(stdin));
close(fd2[0]);
FILE *output = fdopen(fd2[1],"w");
char buffer[255];
while(fgets(buffer,255,stdin)!=NULL)
printf("child: %s",buffer);
// fprintf(output,"%s",buffer);
} else {
close(fd[0]);
close(fd2[1]);
FILE *output = fdopen(fd[1],"w");
char buffer[255];
while(fgets(buffer,255,stdin)!=NULL)
fprintf(output,"%s",buffer);
//FILE *input = fdopen(fd2[0],"r");
//while(fgets(buffer,255,input)!=NULL)
// printf("Parent: %s",buffer);
}
return 0;
}