title: Why is output being written to a pipe being buffered
body: <p>I am writing a program where the parent process needs to be able to communicate with a another child process, so i wrote a function that redirects the child's standard output and input to pipes.</p>
  <pre><code>int create_child_piped_io(int* pipe_in, int* pipe_out, const char* program, char* argv[], char* envp[])
  {
      int stdin_pipe[2];
      int stdout_pipe[2];
      int child;
  
  
      if(pipe(stdin_pipe) &lt; 0)
      {
          return -1;
      }
  
      if(pipe(stdout_pipe) &lt; 0)
      {
          close(stdin_pipe[0]);
          close(stdin_pipe[1]);
      }
      
      child = fork();
      
  
      if(child == 0)
      {
          close(stdin_pipe[1]);
          close(stdout_pipe[0]);
  
          if(dup2(stdin_pipe[0], STDIN_FILENO) &lt; 0)
          {
              close(stdin_pipe[0]);
              close(stdout_pipe[1]);
  
              exit(errno);
          }
  
          if(dup2(stdout_pipe[1], STDOUT_FILENO) &lt; 0)
          {
              close(stdin_pipe[0]);
              close(stdout_pipe[1]);
  
              exit(errno);
          }
  
          close(stdin_pipe[0]);
          close(stdout_pipe[1]);
  
          execve(program, argv, envp);
  
          exit(1);
      }
      else if(child &gt; 0)
      {
          close(stdin_pipe[0]);
          close(stdout_pipe[1]);
  
          *pipe_in = stdin_pipe[1];
          *pipe_out = stdout_pipe[0];
  
          return child;
      }
      else
      {
          close(stdin_pipe[0]);
          close(stdin_pipe[1]);
          close(stdout_pipe[0]);
          close(stdout_pipe[1]);
  
  
          exit(errno);
      }
  
      return child;
  
  }
  
  
  int main()
  {
      int input, output;
      char* argv[] = {&quot;program&quot;};
      char* envp[] = {NULL};
      int cpid = create_child_piped_io(&amp;input, &amp;output, &quot;/home/jaden/dev/c++/pipes/program&quot;, argv, envp);
  
  
      
      char c;
  
  
      while(read(output, &amp;c, 1) == 1)
      {
          printf(&quot;%c&quot;, c);
      }
      printf(&quot;done\n&quot;);
      int status;
      waitpid(cpid, &amp;status, 0);
      
      close(input);
      close(output);
  
  }
  </code></pre>
  <p>this worked fine but i noticed when writing to the stdout from the child process it wouldn't get sent to the pipe immediately. this is the program the child process would run.</p>
  <pre><code>int main()
  {
      // i &lt; 1025 gets sent to pipe after sleep(10)
      // i &gt; 1025 send first 1024 bytes to pipe immediately
      for(int i = 0; i &lt; 1024; i++)
      {
          write(1, &quot;a&quot;, 1);
      }
      
      sleep(10);
      return 0;
  } 
  </code></pre>
  <p>the output would only get sent to the pipe after the child process ended. I tried different amounts of data to send. it turns out every time i write more than 1024 to stdout, it gets sent to the pipe. this leads me to believe that it is being buffered. I don't understand what the why this would be as the pipe is a buffer in memory as well, so it's just buffering the buffer. if this is standard behavior is there any way to turn it off, as i would like to get the data as soon as it is written.</p>
  <p>i am using linux mint 20.3.</p>