title: Pipe buffer size is 4k or 64k?
body: <p>I read in multiple places that the default buffer size for a pipe is 4kB (for instance, <a href="http://linux.die.net/man/7/pipe">here</a>), and my <code>ulimit -a</code> tends to confirm that statement:</p>
  
  <pre><code>$ ulimit -a
  core file size          (blocks, -c) 0
  data seg size           (kbytes, -d) unlimited
  scheduling priority             (-e) 0
  file size               (blocks, -f) unlimited
  pending signals                 (-i) 15923
  max locked memory       (kbytes, -l) 64
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 1024
  pipe size            (512 bytes, -p) 8 // 8 * 512B = 4kB
  POSIX message queues     (bytes, -q) 819200
  real-time priority              (-r) 0
  stack size              (kbytes, -s) 10240
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 1024
  virtual memory          (kbytes, -v) unlimited
  file locks                      (-x) unlimited
  </code></pre>
  
  <p>But when I use a little program to test the buffer size (by writing into the pipe until the write() blocks), I see a limit of 64kB!</p>
  
  <p>See this program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;limits.h&gt;
  
  int main(void)
  {
      int tube[2];
      char c = 'c';
      int i;
  
      fprintf(stdout, "Tube Creation\n");
      fprintf(stdout, "Theoretical max size: %d\n", PIPE_BUF);
      if( pipe(tube) != 0)
      {
          perror("pipe");
          _exit(1);
      }
      fprintf(stdout, "Writing in pipe\n");
      for(i=0;; i++)
      {
          fprintf(stdout, "%d bytes written\n", i+1);
          if( write(tube[1], &amp;c, 1) != 1)
          {
              perror("Write");
              _exit(1);
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>And its output:</p>
  
  <pre><code>$ ./test_buf_pipe 
  Tube Creation
  Theoretical max size: 4096
  Writing in pipe
  1 bytes written
  2 bytes written
  3 bytes written
  4 bytes written
  [...]
  65535 bytes written
  [blocks here]
  </code></pre>
  
  <p>It strongly suggests that the pipe buffer size is actually 64k!
  What is happening here??</p>