title: std::cin is not blocking when process is forked
body: <p>I have a C++ console application which forks itself and closes the main process.
  std::cin in the childprocess is not blocking anymore if a press any key. This results in an endless loop. If I don't fork before then the application behaves as expected.</p>
  
  <p>I have tried different combinations of cin::ignore, cin::fail, cin::clear and close to get this fixed but without success.</p>
  
  <p>I'm using Ubuntu 18.04.</p>
  
  <p>Why this is happening and how can I fix that?</p>
  
  <pre class="lang-cpp prettyprint-override"><code>/* includes */
  #include &lt;iostream&gt;
  #include &lt;unistd.h&gt;
  #include &lt;limits&gt;
  
  void fork_to_background()
  {
      pid_t f_return = fork();
      if (f_return == -1)
      {
          exit(1);
      }
      if (f_return != 0)
      {
          exit(0);
      }
  }
  
  
  int main(int argc, char** argv)
  {
      fork_to_background();
  
      std::string commands;
  
      while(true)
      {
          std::cin &gt;&gt; commands;
          std::cout &lt;&lt; "Loop" &lt;&lt; std::endl;
  
          //std::cin.clear();
          //std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
      }
  }
  </code></pre>