title: Pipe to sort and back to main
body: <p>So i am trying to understand how pipes work in UNIX and i am trying to pipe a text into sort, sort them and pipe them back to main to doo. But when the execution reaches:</p>
  
  <p>Note: The program takes the text file as an argument.</p>
  
  <pre><code>execlp("sort", "sort",(char *)0);
  </code></pre>
  
  <p>The program stops and stays still there like its waiting from the pipe something. I know that there must be something with my understanding of UNIX piping.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  int main (int argc, char **argv){
      int pipe1[2];
      int pipe2[2];
  
      pid_t childpid;
  
      FILE *fdin;
  
      long fsize;
  
      pipe(pipe1);
      pipe(pipe2);
  
  // error handling
      if ((childpid = fork()) == -1){
          perror("fork");
          exit(1);
      }
  
  // parent load file, write to pipe1
      if (childpid != 0){
          char buf[1024];
          close(pipe1[0]);
          close(pipe2[1]);
  
          fdin = fopen(argv[1], "r");
  
          //fseek(fdin, 0, SEEK_END);
          //fsize = ftell(fdin);
          //fseek(fdin, 0, SEEK_SET);
  
          fread(buf, sizeof(buf), 1, fdin);
          fclose(fdin);
  
          dup2(pipe1[1],STDOUT_FILENO);
          write(pipe1[1], buf, sizeof(buf));
          close(pipe1[1]);
      }
      else if (childpid == 0){
          char buf[1024];
          close(pipe1[1]);
          close(pipe2[0]);
          //dup2(pipe2[1], STDOUT_FILENO);
          //dup2(pipe1[0], STDIN_FILENO);
  
          read(pipe1[0], buf, sizeof(buf));
          close(pipe1[0]);
  
          printf("%s\n\n", buf);
  
          dup2(pipe2[1], STDOUT_FILENO);
          close(pipe2[1]);
  
          execlp("sort", "sort",(char *)0);
  
          printf("%s\n", buf);
          exit(0);
      }
  
  // wait child
      wait(NULL);
  
  // parent read pipe 2 and print
      if (childpid != 0){
          // DOOOO
          //read(pipe2[0], buf, 1024);
          //printf("%s\n", buf);
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>Dead Locked</strong>
  <a href="https://i.stack.imgur.com/BRTtJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BRTtJ.jpg" alt="Thought Picture 1 Dead Lock"></a></p>
  
  <p><strong>Updated</strong>
  <a href="https://i.stack.imgur.com/C2yYs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C2yYs.jpg" alt="Thought Picture 2"></a></p>
  
  <p><strong>UPDATE: 1</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;string.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/wait.h&gt;
  
  //char *message = "This is a message!!!";
  
  int main (int argc, char **argv){
      int pipe1[2];
      int pipe2[2];
  
      pid_t childpid[2];
  
      FILE *fdin;
      char buf[1024];
  
      //long fsize;
  
      pipe(pipe1);
      pipe(pipe2);
  
  // error handling
      if ((childpid[0] = fork()) == -1)
      {
          perror("fork");
          exit(1);
      }
  
      // parent load file, write to pipe1
      if (childpid[0] != 0){
  
          close(pipe1[0]);
          close(pipe2[1]);
  
  
          fdin = fopen(argv[1], "r");
  
          //fseek(fdin, 0, SEEK_END);
          //fsize = ftell(fdin);
          //fseek(fdin, 0, SEEK_SET);
  
          fread(buf, sizeof(buf), 1, fdin);
          fclose(fdin);
  
          dup2(pipe1[1],STDOUT_FILENO);
          write(pipe1[1], buf, sizeof(buf));
          close(pipe1[1]);
      }
      else if (childpid[0] == 0){
          buf[0] = '\0';
          int pipe3[2];
  
          pipe(pipe3);
  
          close(pipe1[1]);
          close(pipe2[0]);
  
          //dup2(pipe2[1], STDOUT_FILENO);
          dup2(pipe1[0], STDIN_FILENO);
          //dup2(pipe3[1],STDOUT_FILENO);
  
          read(pipe1[0], buf, sizeof(buf));
          close(pipe1[0]);
  
          write(pipe3[1], buf, sizeof(buf));
          printf("-PIPED BUFF-\n%s\n\n", buf);
  
          if ((childpid[1] = fork()) == -1){
              perror("fork second child");
              exit(1);
          }
          // Child of child (sort call)
          if (childpid[1] != 0){
              close(pipe2[1]);
              close(pipe3[0]);
              printf("I AM YOUR FATHER LOOK\n");
          }else{
              printf("a\n");
              buf[0] = '\0';
              printf("b\n");
              close(pipe3[1]);
              printf("c\n\n");
  
              dup2(pipe3[0], STDIN_FILENO);
              read(pipe3[0], buf, sizeof(buf));
              close(pipe3[0]);
  
              printf("-SORT BUFF-\n%s\n\n", buf);
  
              //dup2(pipe2[1], STDOUT_FILENO);
              close(pipe2[1]);
  
              execlp("sort","sort",(char *)0);
  
              printf("-SORTED BUFF-\n%s\n\n", buf);
              exit(0);
          }
          // wait second child exec
          wait(NULL);
  
          //printf("%s\n", buf);
          exit(0);
      }
  
      // wait child exec
      //wait(NULL);
  
      int status;
      pid_t pid;
      int n = 2;
      while (n &gt; 0){
          pid = wait(&amp;status);
          printf("-SORTED BUFF-\n%s\n\n", buf);
          --n;
      }
      // parent read pipe 2 and print
      if (childpid[0] != 0){
          printf("asd\n");
          buf[0] = '\0';
          dup2(pipe2[0], STDIN_FILENO);
          read(pipe2[0], buf, sizeof(buf));
          close(pipe2[0]);
          printf("-SORTED BUFF-\n%s\n\n", buf);
      }
      return 0;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/HsUHJ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HsUHJ.jpg" alt="**Thought 3**"></a></p>