title: Php cli has stop woriking PHP Pthreads program
body: <p>I have a huge text file. I want to write specific lines from db to the file at specific places. I wrote a php script for this it ran for 17 days. To reduce the time I re-write the program using Php Pthreads as multi threaded program. Now I have divided the file into 100 small files. Now 100 threads are manipulating the files separately. The program runs fine for about a day but the command line gives CLI has stopped working error.</p>
  
  <p>Here are the specifics:  </p>
  
  <blockquote>
    <p>Windows 7 Professional Service Pack 1<br>
    Processor: Intel(R) Core(TM) i5<br>
    Ram: 4 GB<br>
    PHP Version: 5.5.12<br>
    Pthreads Version: 2.0.10  </p>
  </blockquote>
  
  <p>This is the windows log error:</p>
  
  <blockquote>
    <p>Log Name:      Application<br>
    Source:        Application Error<br>
    Date:          7/8/2015 7:41:22 AM<br>
    Event ID:      1000<br>
    Task Category: (100)<br>
    Level:         Error<br>
    Keywords:      Classic<br>
    User:          N/A<br>
    Computer:      ICT-DB-Migr<br>
    Description:<br>
    Faulting application name: php.exe, version: 5.5.12.0, time stamp: 0x53613fb5<br>
    Faulting module name: php5ts.dll, version: 5.5.12.0, time stamp: 0x53614545<br>
    Exception code: 0xc0000005<br>
    Fault offset: 0x000000000000ad68<br>
    Faulting process id: 0x10ec<br>
    Faulting application start time: 0x01d0b889e4ff1462<br>
    Faulting application path: C:\wamp\bin\php\php5.5.12\php.exe<br>
    Faulting module path: C:\wamp\bin\php\php5.5.12\php5ts.dll<br>
    Report Id: a05581e5-2516-11e5-9cc2-a0481c83537b  </p>
  </blockquote>
  
  <p>This is my php program:</p>
  
  <pre><code>&lt;?php
  
  ini_set('display_errors',1);
  ini_set('display_startup_errors',1);
  error_reporting(-1);
  
  /**
   * Author: 
   * URL: 
   */
  
   $time_start = microtime(true); 
  
   class ThreadSafe extends Stackable {
     public function run(){}
   }
  
  
  class WorkerThreads extends Thread
  {
     private $workerId;
     private $conn;   
  
      public function __construct($id,$dataArray)
      {
      $this-&gt;workerId = $id;
      $this-&gt;dataArray2 = $dataArray;
      }
  
  public function run()
  {
  
      include "db_connect.php";
  
  
      $file_name="breaked_mark_files\USMARK_".$this-&gt;workerId.".mrk";     
      $file=fopen($file_name,"r") or exit("Unable to open file!");        
      $id=0;
      $dataCount=-1;
      $count852=0;
      $dataSubCount=0;
  
      while(!feof($file)){    
  
          echo 'worker '.$this-&gt;workerId." executing line record ".$id."    line number".$dataCount."".PHP_EOL;
  
  
  
          $line = fgets($file);   
          $field = substr($line,0,4);
          $strlenth = strlen($line);  
          $data = substr($line,6);    
  
          /* if($dataCount==1){   
  
              //var_dump($dataArray2);
              break;
  
          } */
  
          if($field == "=LDR"){       
  
              $count852=0;
              $dataCount++;   
              $dataSubCoun=0;                 
              $this-&gt;dataArray2[]=$line;
  
              $dataSubCount++;
  
          }elseif($field=="=852"){    
  
              if($count852 == 0){
  
                  $array=explode("\$",$data);
  
                  foreach($array as $key=&gt;$value){
  
                      if($key !=0){
  
                          $str = $value;
                          $strlength=strlen($str);
                          $first_string=substr($str,0,1);
  
                          if($first_string == "p"){
  
                              $id=substr($str,1);                 
  
                          }           
  
                      }
  
                  }
  
                  $this-&gt;dataArray2[]=$line;
                  //$count995++;
                  $dataSubCount++;                    
  
              }else{      
  
                  $this-&gt;dataArray2[]=$line;
                  $dataSubCount++;
              }
  
          }else{
  
              $this-&gt;dataArray2[]=$line;
              $dataSubCount++;    
  
          }   
  
          //$id = intval($id);
          $id = mysql_real_escape_string($id);            
          $sql = "SELECT languageData FROM data_table WHERE id='$id'";
  
  
  
          if($id != 0){               
  
              $result = $conn-&gt;query($sql);           
  
              if ($result-&gt;num_rows &gt; 0) {
  
                  // output data of each row
                  while($row = $result-&gt;fetch_assoc()) {
  
                      $this-&gt;dataArray2[]=$row["languageData"];
                      $dataSubCount++;
  
                  }                   
  
              } else {
                  //echo "0 results of main record for id  ".$id."".PHP_EOL;
              }
  
              $sql = "SELECT holdingData FROM holding_data WHERE record_id='$id'";
  
              $result = $conn-&gt;query($sql);
  
              if ($result-&gt;num_rows &gt; 0) {
                  // output data of each row
                  while($row = $result-&gt;fetch_assoc()) {
  
                      $this-&gt;dataArray2[]=$row["holdingData"];
                      $dataSubCount++;
  
                  }
  
                  $id=0;                  
  
              } else {
  
                  //echo "0 results of holding data for id ".$id."".PHP_EOL;
              }
  
          }   
  
      }
  
      $conn-&gt;close();
    }
  }
  
  // Worker pool
  $workers = [];
  $array=[];
  
  foreach (range(0, 49) as $i){
  
  $array[$i]= new ThreadSafe();   
  }
  
  // Initialize and start the threads
  
  foreach (range(0, 49) as $i) {
  
    $workers[$i] = new WorkerThreads($i,$array[$i]);
    $workers[$i]-&gt;start();
  }
  
  // Let the threads come back
  foreach (range(0, 49) as $i) {
  $workers[$i]-&gt;join();
  }
  
  
  
   $file=fopen("writtenFle.txt","w") or exit("Unable to open file!");
  
   $array_count = count($array);
  
   foreach(range(0, ($array_count-1)) as $i){
  
   $array_count2 = count($array[$i]);
  
   foreach(range(0, ($array_count2-1)) as $y){
  
      fwrite($file,$array[$i][$y]."\r\n"); 
  
    }
  
  }
  
  fclose($file);
  
  $time_end = microtime(true);
  
  $execution_time = ($time_end - $time_start)/60;
  
  echo 'Total Execution Time: '.$execution_time.' Mins'.PHP_EOL;
  </code></pre>