title: Calling a function in a background thread / process (forking)
body: <p>I have code that's somewhat like this:</p>
  
  <pre><code>($i=0; $i &lt; 100; $i++)
  {
     do ($foo[$i]);
  }
  </code></pre>
  
  <p>The above is a time intensive task, and I was hoping to be able to create a function, and call it twice like below</p>
  
  <pre><code>function wrapper($start;$end)
  {
      ($i=$start; $i &lt; $end; $i++)
      {
         do ($foo[$i]);
      }
  }
  //have both of these run in parallel
  wrapper(0,50);
  wrapper(51,100);
  </code></pre>
  
  <p>I looked at Gearman but I cannot use it as I cannot install the gearman server (since I'm on a shared server). It seems like the way to achieve this would be by forking. I tried reading up a lot about it, but documentation and support is scant. Any help / wireframe code would be appreciated.</p>
  
  <p>To define my question, how could I call <code>wrapper()</code> passing in arguments such that it executes in a child process. Also, its important that I be able to register a callback function.</p>
  
  <p>Additional Details: PHP 5.3, running on Linux server. Script is executed by cgi-fcgi.</p>
  
  <p>I think this is how I am supposed to spawn a child process, but how can I use it to spawn multiple child processes? And how do I register a callback function?</p>
  
  <pre><code>$pid = pcntl_fork(); 
  
  if ( $pid == -1 ) {        
      // Fork failed            
      exit(1); 
  } else if ( $pid ) { 
      // The parent process
      //should I repeat this same code here to spawn another child process?
  
  } else { 
      // the child process 
      //can I call wrapper from here and will it run in this child process?
  </code></pre>