title: Windows: how can I let the child process read the input anonymous pipe without closing it?
body: <p>As per subject I'm trying to develop a simple piped parent/child program.
  Main purpose of this program is to keep the child process alive and use <code>std::cin</code> and <code>std::cout</code> to communicate between parent/child processes.</p>
  
  <p>On Linux all of this works quite well.</p>
  
  <p>On Windows I've been following the example <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx" rel="nofollow">here</a> and there's one peculiar difference with Linux: one has to invoke</p>
  
  <pre><code>CloseHandle(g_hChildStd_IN_Wr)
  </code></pre>
  
  <p>To write to the child pipe and flush it. This has the side effect to close the pipe, thus terminating my <em>in-connection</em> to the child process.<br>
  I've also tried to use <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa364439(v=vs.85).aspx" rel="nofollow">FlushFileBuffers</a> but it doesn't work.</p>
  
  <p>Any idea how can I <em>flush</em> the buffer without having to close the <em>anonymous pipe</em>?</p>
  
  <p>Below sources of both <em>Parent</em> and <em>Child</em> processes.<br>
  If the code of the <em>parent</em> process is basically the one in the example above:</p>
  
  <pre><code>// IN_Wr_ is initialized as below with bInheritHandle=TRUE
  ::CreatePipe(&amp;IN_Rd_, &amp;IN_Wr_, &amp;saAttr, 0);
  // and
  ::SetHandleInformation(IN_Wr_, HANDLE_FLAG_INHERIT, 0)
  // When I spawn the child process I do
  STARTUPINFO         siStartInfo = {0};
  siStartInfo.cb = sizeof(STARTUPINFO); 
  siStartInfo.hStdError = INVALID_HANDLE_VALUE;
  siStartInfo.hStdOutput = OUT_Wr_;
  siStartInfo.hStdInput = IN_Rd_;
  siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
  ...
  // then in order to write to std::cin
  const DWORD reqSz = static_cast&lt;DWORD&gt;(std::strlen(request));
  DWORD       written = 0;
  while(true) {
      DWORD   curWritten = 0;
      if(!WriteFile(IN_Wr_, request + written, reqSz-written, &amp;curWritten, NULL))
          throw std::runtime_error("Error on WriteFile");
      written += curWritten;
      if(written == reqSz) {
          // all written, done
          break;
      }
  }
  ::FlushFileBuffers(IN_Wr_);
  // only when I do this CloseHandle then the child process
  // is able to read data
  ::CloseHandle(IN_Wr_);
  </code></pre>
  
  <p>this <em>child</em> code is a simple <em>echo</em> server, along the lines of:</p>
  
  <pre><code>buif[2048+1];
  while(std::cin) {
      std::cin.read(buf, 2048);
      const auto  rb = std::cin.gcount();
      buf[rb] = '\0';
      std::cout &lt;&lt; buf &lt;&lt; std::endl; // this does flush
  }
  </code></pre>