title: pcntl runs the same code several times, assistance required
body: <p>I am using pcntl in order to speed up a quite heave CLI php script, that consists mostly of a class, that is in charge of sending all of the auto-emailing on my application.</p>
  
  <p>My goal is as following:
  I want to assign each process to a certain task, within a foreach loop, the implementation I've used is the one shown in the code example below.</p>
  
  <p>The problem is that once you fork a process, it executes asynchronously, and also gets a copy of the parent's process stack.
  <strong>In my case, what happens is that one task simply executes several times,</strong> 
  My question is how can I design this script to be smarter in order to avoid such behavior?.</p>
  
  <p>Code:</p>
  
  <pre><code>/**
  @description this is the main procedure of this class, it iteratates over the relevant tasks and sends the emails using the SendGrid wrapper class
  @see SendGridWrapper
  @return void
  */
  public function execute(){
      if(!isset($this-&gt;tasks)){
          throw new exception("Please call getRelevantTasks() prior to trying to execute anything");
      }
  $proccesses = array();
  foreach($this-&gt;tasks as $myTask){
      $pid = pcntl_fork();
      if($pid){
          $proccesses[] = $pid;
      }
      else if($pid == -1){
          die('FORK FAILED, STATUS -1');
      }
      else{   
          if(isset($myTask['recipient_model'])){
  
              $this-&gt;currentModel = $myTask['recipient_model'];
              $lang = $myTask['lang'];
              $classPath = self::$modelsDir . $myTask['recipient_model'] . '.php';
              $className = $myTask['recipient_model'];
              if(!class_exists($myTask['recipient_model'] )){
                  require_once(dirname(__FILE__) . '/../' .  $classPath); 
              }
              else if(isset($recipientFetcher)){
                  unset($this-&gt;model);
                  unset($this-&gt;mailingList);
                  unset($this-&gt;substitutionList);
              }
              $this-&gt;model = null;
              $this-&gt;mailingList = null;
              $this-&gt;substitutionList = null;
              $this-&gt;model = new $className($myTask['lang']);
              $addresses = $this-&gt;model-&gt;getMailRecipients();
              if(empty($addresses) || sizeof($addresses) == 0){
                  continue;
              }
              $this-&gt;model-&gt;prepare();
              $this-&gt;substitutionList = $this-&gt;model-&gt;getDynamicParams();
  
          }
          else{
              throw new exception('No recipient model was found');
          }
  
          foreach($addresses as $myMail){
              $this-&gt;mailingList[$myMail['personal_email']] = $myMail['contact_name'];
          }
          $templatePath = dirname(__FILE__) . '/../'; 
          $templatePath .= $lang ? self::$templatesDirEn . $myTask['html_email_path'] : self::$templatesDirHe . $myTask['html_email_path'];
          $html = file_get_contents($templatePath);
          $this-&gt;sendMail($html, $myTask['task_schedule_id']);
          echo "model:" . $myTask['recipient_model'];
          echo $this-&gt;log;
          $this-&gt;log = "";
          die("\r\n Child proccess has been executed successfully\r\n");  
  
      }   
  }
  if($pid){
      foreach($proccesses as $key =&gt; $val){
           pcntl_waitpid($val, $status, WUNTRACED);
      }
  }           
  </code></pre>
  
  <p>}   </p>
  
  <p>Thanks in advance,
  Oleg.</p>