title: Redirect child process's stdin and stdout to pipes
body: <hr>
  
  <p>EDIT:
  The solution is </p>
  
  <pre><code> int c1=dup2(pipes[0][1],STDOUT_FILENO);
   int c2=dup2(pipes[1][0],STDIN_FILENO);
  
  
   setvbuf(stdout,NULL,_IONBF,0);
  </code></pre>
  
  <p>It is SETVBUF to set stdout to be non-buffered. Even though I was printing the newline character if the destination is not an actual screen, I guess, it becomes buffered. </p>
  
  <hr>
  
  <hr>
  
  <p>EDIT:
  When I put <strong>fflush(stdout)</strong> after LINE 1 and <strong>fflush(fout)</strong> after LINE 4 it works as expected. However, it does not work without the <strong>fflush(stdout)</strong> after LINE 1. The problem is that I would not be able to put <strong>fflush</strong> in the program which I am planning to run.</p>
  
  <hr>
  
  <p>I am trying to start another program from my process. I don't have access to its code but I know it uses stdin and stdout for user interaction. I am trying to start that program by creating 2 pipes, fork-ing and redirecting the child's stdin/stdout to the proper pipe ends. The points is that the parent should be able communicate with the child via file descriptors, while its stdin/stdout should be intact. The POPEN syscall only opens unidirectional pipe. The following code almost works. </p>
  
  <p>There are 4 lines marked as LINE 1..4.</p>
  
  <p>LINE 1 is child sending to pipe,
  LINE 2 is child receiving from pipe,
  LINE 3 is parent sending to pipe,
  LINE 4 is parent receiving from pipe,</p>
  
  <p>This is just a toy example to make sure things work. The issue is that is all 4 lines LINE1..4 are uncommented the output I see on the terminal is </p>
  
  <pre><code>PARENT1: -1
  FD: 1 0    4 5    0 1
  DEBUG1: 0
  DEBUG2: 0
  </code></pre>
  
  <p>While if LINE 1 and LINE 3 are uncommented only I see a continuous stream of data. Same happens if if only LINE 2 and LINE 4 are uncommented. However, I want a full bidirectional communication. Also adding the commented SLEEP does not change the behavior. </p>
  
  <p>What could be the issue here. I wonder why is there no bidirectional POPEN.</p>
  
  <pre><code>int pid;
  int pipes[2][2];
  
  pipe(pipes[0]);
  pipe(pipes[1]);
  
  pid=fork();
  
  if(pid==0)
    {
    //usleep(1000000);
    close(pipes[0][0]);
    close(pipes[1][1]);
  
    int c1=dup2(pipes[0][1],STDOUT_FILENO);
    int c2=dup2(pipes[1][0],STDIN_FILENO);
    //int c2=dup2(STDIN_FILENO,pipes[1][0]);
  
    fprintf(stderr,"FD: %d %d    %d %d    %d %d\n",c1,c2,pipes[0][1],pipes[1][0],STDIN_FILENO,STDOUT_FILENO);
  
    //FILE*fout=fdopen(pipes[0][1],"w");
    //FILE*fin =fdopen(pipes[1][0],"r");
    while(1)
      {
      static int c1=0;
      fprintf(stderr,"DEBUG1: %d\n",c1);
      printf("%d\n",c1);                      // LINE 1
      fprintf(stderr,"DEBUG2: %d\n",c1);
      scanf("%d",&amp;c1);                        // LINE 2
      fprintf(stderr,"DEBUG3: %d\n",c1);
      c1++;
      }
    //fclose(fout);
    //fclose(fin);
    return 0;
    }
  
  close(pipes[0][1]);
  close(pipes[1][0]);
  
  char buffer[100];
  FILE*fin=fdopen(pipes[0][0],"r");
  FILE*fout=fdopen(pipes[1][1],"w");
  while(1)
    {
    int c1=-1;
    printf("PARENT1: %d\n",c1);
    fscanf(fin,"%d",&amp;c1);                         // LINE 3
    printf("Recv: %d\n",c1);
  
    fprintf(fout,"%d\n",c1+1);                    // LINE 4
    printf("PARENT3: %d\n",c1+1);
    }
  fclose(fin);
  fclose(fout);
  </code></pre>