title: writing to a pipe with a child and parent process
body: <p>I am trying to create a child that calls sort. The parent sends data to the child through a pipe. My code compiles and runs, but there is no output. What am I doing wrong? Am I not closing the pipes correctly, writing the pipes or outputting the data correctly?  </p>
  
  <p>[eddit] On my system I need to call /bin/sort NOT /usr/bin/sort!</p>
  
  <pre><code>#include &lt;sys/wait.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;ctype.h&gt;
  
  int main(void){
  int pipes[2];
  pid_t pid;
  FILE *stream;
  
  if(pipe(pipes) == -1)
      printf("could not create pipe\n");
  
  switch(fork()){
      case -1:
          fprintf(stderr, "error forking\n");
          break;
      case 0:
          dup2(pipes[0], STDIN_FILENO);
  
          pid = getpid();
          printf("in child, pid=%d\n");
  
          if(close(pipes[1]) == -1)
              fprintf(stderr,"err closing write end pid=%d\n", pid);
  
          execl("/usr/bin/sort", "sort",  (char*) NULL);
          break;
      default:
          stream = fdopen(pipes[1], "w");
          pid = getpid();
          printf("in parent, pid=%d\n", pid);
  
          if (stream == NULL)
              fprintf(stderr, "could not create file streami\n");
  
          if(close(pipes[0]) == -1)
              printf("err closing read end pid=%d\n");
  
                     fputs("bob\n",stream);
          fputs("cat\n",stream);
          fputs("ace\n",stream);
          fputs("dog\n",stream);
  
          if(fclose(stream) == EOF)
              fprintf(stderr, "error while closing stream\n");
          break;
      }
      return 0;
  }
  </code></pre>
  
  <p>[edit] Here is my working code. Thank you everyone</p>
  
  <pre><code>#include &lt;sys/wait.h&gt;
  #include &lt;sys/stat.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  int main(void){
  int pipes[2];
  pid_t pid;
  FILE *stream;
  
  int stat;
  if(pipe(pipes) == -1)
      printf("could not create pipe\n");
  
  switch(fork()){
      case -1:
          fprintf(stderr, "error forking\n");
          break;
      case 0:
          dup2(pipes[0], STDIN_FILENO);
  
          pid = getpid();
          printf("in child, pid=%d\n", pid);
  
          if(close(pipes[1]) == -1)
              fprintf(stderr,"err closing write end pid=%d\n", pid);
  
          if(close(pipes[0]) == -1)
              fprintf(stderr,"err closing write end pid=%d\n", pid);
  
          execl("/bin/sort", "sort",  (char*) NULL);
          exit(EXIT_FAILURE);
          break;
      default:
          stream = fdopen(pipes[1], "w");
          pid = getpid();
          printf("in parent, pid=%d\n", pid);
  
          if (stream == NULL)
              fprintf(stderr, "could not create file streami\n");
  
          if(close(pipes[0]) == -1)
              printf("err closing read end pid=%d\n");
  
          fputs("bob\n",stream);
          fputs("cat\n",stream);
          fputs("ace\n",stream);
          fputs("dog\n",stream);
  
          if(fclose(stream) == EOF)
              fprintf(stderr, "error while closing stream\n");
          break;
  }
  
  wait(&amp;stat);
  return 0;
  }   
  </code></pre>