PHP_Fork
[ class tree: PHP_Fork ] [ index: PHP_Fork ] [ all elements ]

Source for file passing_vars.php

Documentation is available at passing_vars.php

  1. <?php
  2. /**
  3.  * PHP_Fork class usage examples
  4.  * ==================================================================================
  5.  * NOTE: In real world you surely want to keep each class into
  6.  * a separate file, then include() it into your application.
  7.  * For this examples is more useful to keep all_code_into_one_file,
  8.  * so that each example shows a unique feature of the PHP_Fork framework.
  9.  * ==================================================================================
  10.  * passing_vars.php
  11.  *
  12.  * This example shows variable exchange between the parent process
  13.  * and started pseudo-threads. This was not possible in previous releases because
  14.  * parent and child processes lives into different memory spaces,
  15.  * they are separate processes with their own PID and not separate instances living
  16.  * into the same JVM; this is just forking, not real threading...
  17.  * This framework offers a workaround based Shared Memory usage.
  18.  *
  19.  * ==================================================================================
  20.  *
  21.  */
  22.  
  23. // Import of base class
  24. require_once ("PHP/Fork.php");
  25.  
  26. // number of executeThreads we want
  27. define ("NUM_THREAD"2);
  28.  
  29. // Class definition; as into previos example (basic.php), this class simply
  30. // increment a counter each second; instead of printing the value to stdout,
  31. // we'll store it into an accessible location for latter use.
  32. class executeThread extends PHP_Fork {
  33.     var $counter;
  34.  
  35.     function executeThread($name)
  36.     {
  37.         $this->PHP_Fork($name);
  38.         $this->counter = 0;
  39.     }
  40.  
  41.     function run()
  42.     {
  43.         while (true{
  44.             // setVariable is a method inherited from PHP_Fork super class
  45.             // it sets a variable that can be accessed thru its name
  46.             // by parent process (calling the getVariable() method)
  47.             $this->setVariable('counter'$this->counter++);
  48.             sleep(1);
  49.         }
  50.     }
  51.  
  52.     function getCounter()
  53.     {
  54.         // parent process can call this facility method
  55.         // in order to get back the actual value of the counter
  56.         return $this->getVariable('counter');
  57.     }
  58. }
  59.  
  60. // Main program. Bring up NUM_THREAD instances of the executeThread class that
  61. // runs concurrently. It's a multi-thread app with a few lines of code!!!
  62. // Into this example we have a console to control thread behaviour and test
  63. // their counter value.
  64. for ($i = 0;$i NUM_THREAD;$i++{
  65.     $executeThread[$i= new executeThread ("executeThread-" $i);
  66.     $executeThread[$i]->start();
  67.     echo "Started " $executeThread[$i]->getName(" with PID " $executeThread[$i]->getPid("...\n";
  68. }
  69.  
  70. echo "This is the main process.\nPress [X] to terminate, [G] to read pseudo-thread's counter.\n";
  71.  
  72. // Console simple listener
  73. while (true{
  74.     echo ">";
  75.  
  76.     $opt _getInputCLI();
  77.     echo "\n";
  78.     switch ($opt{
  79.         case "X":
  80.             // stops all threads
  81.             for ($i = 0;$i NUM_THREAD;$i++{
  82.                 $executeThread[$i]->stop();
  83.                 echo "Stopped " $executeThread[$i]->getName("\n";
  84.             }
  85.             exit;
  86.             break;
  87.         case "G":
  88.             for ($i = 0;$i NUM_THREAD;$i++{
  89.                 echo $executeThread[$i]->getName(" returns " $executeThread[$i]->getCounter("\n";
  90.             }
  91.             break;
  92.     }
  93. }
  94. // Functions used by the console
  95. function _getInputCLI()
  96. {
  97.     $opt _read();
  98.     $opt strtoupper (trim($opt));
  99.     return $opt;
  100. }
  101.  
  102. function _read()
  103. {
  104.     $fp fopen("php://stdin""r");
  105.     $input fgets($fp255);
  106.     fclose($fp);
  107.  
  108.     return $input;
  109. }
  110.  
  111. ?>

Documentation generated on Mon, 11 Mar 2019 15:41:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.