title: Why does reading from Process' InputStream block altough data is available
body: <p>Java:</p>
  
  <pre><code>Process p = Runtime.getRuntime().exec("myCommand");
  final InputStream in = p.getInputStream();
  
  new Thread()
  {
      public void run()
      {
          int b;
          while ((b = in.read()) != -1) // Blocks here until process terminates, why?
              System.out.print((char) b);
      }
  }.start();
  </code></pre>
  
  <p>CPP:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  
  int main(int argc, char** argv)
  {
      printf("round 1\n");
  
      // At this point I'd expect the Java process be able
      // to read from the input stream.
  
      sleep(1);
  
      printf("round 2\n");
  
      sleep(1);
  
      printf("round 3\n");
  
      sleep(1);
  
      printf("finished!\n");
  
      return 0;
  
      // Only now InputStream.read() stops blocking and starts reading.
  }
  </code></pre>
  
  <p>The documentation of InputStream.read() states:</p>
  
  <blockquote>
    <p>This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.</p>
  </blockquote>
  
  <p>And yes, I'm aware of this (thus linux related?):</p>
  
  <blockquote>
    <p><strong>java.lang.Process:</strong> Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.</p>
  </blockquote>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>Why is InputStream.read() blocking although I should already have data available right after the process starts? Am I missing something on either side?</p></li>
  <li><p>If it's linux related, is there any way to read from the process' output stream without blocking?</p></li>
  </ol>