Another multi-process implementation method under the CLI----pcntl

Source: Internet
Author: User
Tags bz2 fpm

Sometimes you need to optimize some scripts in order to run faster and accomplish tasks in less time. Pcntl is a good choice, it can fork multiple processes to work together to complete a task, the theoretical completion of the time will be inversely proportional to the number of processes.

However, PCNTL can only be used in CLI mode and cannot be used in a Web server environment, otherwise it may cause unexpected errors and is not recommended.

    • To install the PCNTL extension:

      wget http://cn2.php.net/distributions/php-5.3.27.tar.bz2
      TAR-JXVF php-5.3.27.tar.bz2
      CD php-5.3.27/ext/pcntl/
      Phpize &&/configure && make install
      echo "extension=pcntl.so" >>/application/php5.3.27/lib/php.ini
      KILL-USR2 ' Cat/var/run/php-fpm.pid '
      Download--Unzip---Add extension module, re-start PHP-FPM

      Note: In the online Web server, PHP compile time has integrated this function, do not need to add separately, otherwise it will be reported warning.

    • Program Examples:

      The procedure is described in more detail in the comments, which are no longer described separately.

    • Execution Result:

      As can be seen from the results, although the process of the fork in the program is ordered, but in actual execution is not necessarily orderly, the process of the completion of the sequence and the process started is not consistent.
      Before fork, a child process can inherit something from the parent process, but after pcntl_fork () the child process and the parent process have no inheritance relationship. What is created in a child process is a child process, and what is created in the parent process is the parent process, which can be considered entirely as two separate processes.
      Fork, the program out of bifurcation, the derivation of two processes, the specific which first run, which is the end of the system to see the scheduling algorithm.

    • Demo
    • <?PHP/** * PCNTL Test * @author: xxxx*/Set_time_limit(0);//if the Pcntl_fork function is not found, exit directlyif(!function_exists(' Pcntl_fork '))Echo"Pcntl functions not available in this PHP installation\n";//start of script run$start= Time();Echo"\nscrit RUN at:",Date(' Y-m-d h:i:s ',$start), "\ n";//take parameters from CLI//default run of 10 processes$pmax=Empty($argv[1])? 10:$argv[1];//parent Process PID$ppid=Getmypid();  for($i= 1;$i<=$pmax; ++$i) {    //start generating child processes    $pid=pcntl_fork (); Switch($pid) {     Case-1://Fork Failed        Echo"Fork failed!\n";  Break;  Case0://Fork succeeds, and the child process will enter here        Sleep(1); $cpid=Getmypid();//get the PID of the current process with the Getmypid () function        Echo"Fork:child #{$i} #{$cpid} is running...\n "; //child process to exit otherwise a recursive multi-process is performed, and the parent process does not exit or terminate the multi-process        Exit($i);  Break; default://Fork succeeds, and the parent process will enter here        if($i= = 1) {            EchoThe Parent #{$ppid} is running...\n "; }         Break; }}//The parent process takes advantage of the while loop and waits until all child processes are completed by the Pcntl_waitpid function to continue down while(Pcntl_waitpid (0,$status)! =-1) {    //Pcntl_wexitstatus Returns the return code of an interrupted subprocess, which can be used to determine which child process has completed    $status= Pcntl_wexitstatus ($status); EchoThe child$statusHas completed!\n ";}EchoThe Parent #{$ppid} has completed!\n ";Echo"\nscrit END at:",Date(' Y-m-d h:i:s ',$start), "\ n";Echo"Total timeeee:". ( Time() -$start)/60;Echo"\n++++++++++++++++++++++++++++++++++++++++++++ok++++++++++++++++++++++++++++++++++++++++++++++++++\n";?>

Another multi-process implementation method under the CLI----pcntl

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.