title: Understanding OpenMP shortcomings regarding fork
body: <p><strong><em>I wish to understand what do they mean here. Why would this program "hang"?</em></strong></p>
  
  <p>From <a href="https://bisqwit.iki.fi/story/howto/openmp/" rel="noreferrer">https://bisqwit.iki.fi/story/howto/openmp/</a>  </p>
  
  <blockquote>
    <p>OpenMP and <code>fork()</code> It is worth mentioning that using OpenMP in a
    program that calls <code>fork()</code> requires special consideration. This
    problem only affects GCC; ICC is not affected.   If your program
    intends to become a background process using <code>daemonize()</code> or other
    similar means, you must not use the OpenMP features before the fork.
    After OpenMP features are utilized, a fork is only allowed if the
    child process does not use OpenMP features, or it does so as a
    completely new process (such as after <code>exec()</code>).</p>
    
    <p>This is an example of an erroneous program:</p>
  
  <pre><code>#include &lt;stdio.h&gt;   
  #include &lt;sys/wait.h&gt;   
  #include &lt;unistd.h&gt;
  
  void a(){
      #pragma omp parallel num_threads(2)
      {
          puts("para_a"); // output twice
      }
      puts("a ended"); // output once   
  }
  
  void b(){
      #pragma omp parallel num_threads(2)
      {
          puts("para_b");
      }
      puts("b ended");   
  }
  
  int main(){    
      a();   // Invokes OpenMP features (parent process)   
      int p = fork();    
      if(!p){
          b(); // ERROR: Uses OpenMP again, but in child process
          _exit(0);    
      }    
      wait(NULL);    
      return 0;   
  }
  </code></pre>
    
    <p>When run, this program hangs, never reaching the line that outputs "b
    ended". There is currently no workaround as the libgomp API does not
    specify functions that can be used to prepare for a call to <code>fork()</code>.</p>
  </blockquote>