2

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;
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
TimNeutron
  • 115
  • 1
  • 2
  • 9

3 Answers3

2

The parent needs to close its side of the pipe to the child so that the child will detect end-of-file and terminate.

  while(fgets(buffer,255,stdin)!=NULL)
      fprintf(output,"%s",buffer);
  fclose(output); // does close(fd[1]);
  FILE *input = fdopen(fd2[0],"r");
  while(fgets(buffer,255,input)!=NULL)
    printf("Parent: %s",buffer);
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
2

When you have both input and output pipes between two (single-threaded) processes, you can have some deadlock, so you need to have an event loop using a multiplexing syscall (generally poll(2)...) and you will either read or write, depending on what is possible. Of course you need to buffer! BTW, in that case, you'll better use low level syscalls(2) without using <stdio.h> (and if you still do use stdio, don't forget to fflush(3)....). See also this answer.

(of course I am supposing a POSIX or Linux system)

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

Make sure everything gets closed. After

dup2(fd[0],fileno(stdin));

you should do:

close(fd[0]);
William Pursell
  • 204,365
  • 48
  • 270
  • 300