title: Fork and dup2 - Child process is not terminating - Issues with file descriptors?
body: <p>I am writing my own shell for a homework assignment, and am running into issues.</p>
  
  <p>My shell program gets an input <code>cat scores | grep 100</code> from the console <strong><em>and prints the output as expected but the <code>grep</code> command doesn't terminate and I can see it running infinitely from <code>ps</code> command.</em></strong></p>
  
  <hr>
  
  <p><strong>EDIT - There was an error while closing fds. Now grep command is not executing and console output is -</strong> </p>
  
  <p><strong><em>grep: (standard input): Bad file descriptor</em></strong></p>
  
  <hr>
  
  <p>I am reading the number of commands from the console and creating necessary pipes and storing them in a two dimensional <code>int array fd[][]</code> before forking the first process.</p>
  
  <p><code>fd[0][0]</code> will contain read end of 1st pipe and <code>fd[0][1]</code> will contain write end of 1st pipe. <code>fd[1][0]</code> will contain read end of 2nd pipe and <code>fd[1][1]</code> will contain write end of 2nd pipe and so on.</p>
  
  <p>Each new process duplicates its <code>stdin</code> with the read end of its pipe with the previous process and duplicates its <code>stdout</code> with the write end of its pipe with the next process.</p>
  
  <p>Below is my function:</p>
  
  <pre><code>void run_cmds(char **args, int count,int pos)
  {
      int pid,status;
      pid = fork();
      if ( pid == 0 )
      {
          if(pos != 0) dup2(fd[pos-1][0],0); // not changing stdin for 1st process
          if(pos != count) dup2(fd[pos][1],1); //not changing stdout for last process
          close_fds(pos);
          execvp(*args,args);
      }
      else
      {
          waitpid(pid,&amp;status,0);
          count--;
          pos++;
          //getting next command and storing it in args
          if(count &gt; 0)
              run_cmds(args,count,pos);
          }
      }
  }
  </code></pre>
  
  <ul>
  <li><strong>args</strong> will contain the arguments for the command. </li>
  <li><strong>count</strong> is the number of commands I need to create. </li>
  <li><strong>pos</strong> is the position of the command in the input</li>
  </ul>
  
  <p>I am not able to figure out the problem. I used this same approach for hard coded values before this and it was working.</p>
  
  <p>What am I missing with my understanding/implementation of <code>dup2</code>/<code>fork</code> and why is the command waiting infinitely?</p>
  
  <p>Any inputs would be greatly helpful. Struck with this for the past couple of days!</p>
  
  <hr>
  
  <p>EDIT : close_fds() function is as below -
  For any process , I am closing both the pipes linking the process.</p>
  
  <pre><code>void close_fds(int pos)
  {
   if ( pos != 0 )
          {
          close(fd[pos-1][0]);
          close(fd[pos-1][1]);
          }
   if ( pos != count) 
          {
          close(fd[pos][0]);  
          close(fd[pos][1]);
          }
  }
  </code></pre>