title: Implementing pipe in c.But it is not working
body: <p>I am trying to implement the pipes using c language.
  I am executing a loop for every command separated by a pipe by creating child process</p>
  
  <p>Following is my code:</p>
  
  <pre><code>int main(int argc, char **argv)
  {
      char *cmd,*splitcmd;
      int i,j,nargc=0,characters;
      char **cmdArray;
      size_t bufsize = 1024;
      int *pipefd;
      int pipeArrCount;
      pid_t pid,wpid;
      int status = 0;
  
      int savestdoutfd = dup(fileno(stdout));
      int savestdinfd = dup(fileno(stdin));
  
      cmd = (char *)malloc(bufsize * sizeof(char));
      characters = getline(&amp;cmd,&amp;bufsize,stdin);
  //    printf("%s%d",cmd,characters);
      if(cmd[characters-1]=='\n')
      {
  //        printf("in c");
          cmd[characters-1]='\0';
          characters--;
      }
  
      cmdArray = (char**)malloc(sizeof(char *) * 100);
      splitcmd=strtok(cmd,"|");
  //    printf("%s\n",cmd);
      while((splitcmd))
      {
          cmdArray[nargc] = splitcmd;
          if(cmdArray[nargc][(strlen(cmdArray[nargc]))-1]==' ')
              cmdArray[nargc][(strlen(cmdArray[nargc]))-1]='\0';
          printf("%d    %s",nargc,cmdArray[nargc]);
          nargc++;
          splitcmd = strtok(NULL,"|");
      }
  
      pipefd=(int*)malloc(2*nargc*sizeof(int));
      printf("%d\n",nargc);
      pipeArrCount=2*(nargc-1);
      printf("%d\n",pipeArrCount);
      //exit(0);
      for(i=0;i&lt;pipeArrCount;i)
      {
          printf("making pipe for process %d\n",i);
          pipe(pipefd+i);
          i=i+2;
      }
  
  
      //exit(0);
      for(i=0;i&lt;nargc;i)
      {
          printf("parent count %d\n",i);
          if(i==0)
          {
              printf("Creating child %d\n",i);
              // As it is first process we need to make write end of the pipe as stdout.
              if ((pid=fork()) == 0)
              {
                  printf("Creating first child %d for command %s\n",i,cmdArray[i]);
                  printf("EXECUTING FIRST PROCESS\n");
                  printf("Writing in pipe[%d]\n",2*i+1);
  
                  //close(pipefd[0]);
  
                  dup2(pipefd[2*i+1],fileno(stdout));
                  //closing all other pipes
                  for(j=0;j&lt;(2*nargc);j++)
                  {
                      //if(j!=2*i+1)
                          close(pipefd[j]);
                  }
                  system(cmdArray[i]);
                  //dup2(savestdoutfd,fileno(stdout));
                  printf("Stdout is again restored\n");
                  //printf("pipe [%d] contains %d ",2*i+1,pipefd[2*i+1]);
                  exit(0);
              }
  
          }
          else if(i!=nargc-1)
          {
              if (fork() == 0)
              {
                  printf("EXECUTING MIDDLE PROCESS\n");
                  printf("Command to execute %s \n",cmdArray[i]);
                  printf("Reading from pipe[%d]\n",(2*(i-1)));
                  printf("writing on pipe[%d]\n",(2*i)+1);
                  dup2(pipefd[(2*(i-1))], 0); //Read end of the previous process pipe as stdin
                  dup2(pipefd[(2*i)+1], 1); //Write end of the pipe of current process as stdout
                  //closing all other pipes
                  for(j=0;j&lt;(2*nargc);j++)
                  {
                      //if((j!=(2*(i-1))) &amp;&amp; (j!=(2*i)+1))
                          close(pipefd[j]);
                  }
                  system(cmdArray[i]);
                  exit(0);
              }
          }
          else
          {
              if (fork() == 0)
              {
                  printf("Creating last child %d for command %s\n",i,cmdArray[i]);
                  printf("Reading from pipe[%d]\n",(2*(i-1)));
  
                  //close(pipefd[1]);
  
                  dup2(pipefd[(2*(i-1))],fileno(stdin)); //Read from the end of the previous process pipe as stdin
                  //printf("EXECUTING LAST PROCESS\n");
                  //closing all other pipes
                  for(i=0;j&lt;(2*nargc);j++)
                  {
                      //if(j!=(2*(i-1)))
                          close(pipefd[j]);
                  }
                  dup2(savestdoutfd,fileno(stdout));
                  close(savestdoutfd);
                  system(cmdArray[i]);
                  dup2(savestdinfd,fileno(stdin));
                  close(savestdinfd);
                  exit(0);
              }
          }
          i=i+1;
          sleep(1);
      }
      while ((wpid = wait(&amp;status)) &gt; 0);
  }
  </code></pre>
  
  <p>Not getting any output of the second command 
  For now i am trying to execute this 'A | B ' type of commands
  space between the command and pipes is necessary if you execute the code</p>