PHP Multi-process

Source: Internet
Author: User
Tags echo date

PHP multi-process using Pcntl_* series functions

I now know a pcntl_fork (), generating a child process

<?PHPHeader("content-type:text/html; Charset=utf8 "); /*Echo ' Current program has been run here! ‘ .    "\ n";    $temp = 1;    Start sub-Process $pid = Pcntl_fork (); if ($pid = =-1) exit (' program Error!    ‘);        if ($pid) {echo "I am the parent process, the current temp value is $temp\n";    $temp = 0;        }else {sleep (1);        if ($temp = = 0) echo "I am the child process, the current temp value is $temp\n";    $temp = 1;        Run Result: The current program has been run here! I am the parent process, the current temp value is 1 Description: The child process is not executed, and the parent process is basically executed concurrently with the child process. And the program runs only once. So only the parent process can be executed*/    /*--------------------------*/    /*    When the current time is greater than the time set, give a Get value if (Strtotime ()-(' 2014/08/05 16:02:00 ')) {$_get[' a '] = 1;    } $temp = false; Echo ' Current program has been run here! ‘ .    "\ n";    Start sub-Process $pid = Pcntl_fork ();    Var_dump ($_get);    echo "Above is the value of the current get \ n"; if ($pid = =-1) exit (' program Error!    ‘);            if ($pid) {//Parent process dead Loop listener do{if (isset ($_get[' a ')) {$temp = true;                }else{echo "I am the parent process, has been running in a loop \ n"; echo Date (' y-m-d h:i:s ').            "\ n";    }}while (True); }else {while ($temp) {echo "I am a child process, the parent process changed the value of temp, I started to execute!"            ";        $temp = false;        }} Result: The current program has been run here! Array (1) {["a"]=> int (1)} above is the value of the current Get Array (1) {["a"]=> I NT (1)} above is the value of the current get description: The program forks after the $pid, the following program executes 2 times, but the get value is true, all parent processes change the value of temp, but the child process also runs        (The value of temp is run before it is changed by the parent process), so the child process is not allowed. Similar to the above warning: System Resource usage is large, similar to the above     */    /*--------Net pick code define ("PC", 10); Number of processes define ("to", 4); Timeout define ("TS", 4);    Event span for simulating task delay if (!function_exists (' pcntl_fork ')) {die ("Pcntl_fork not existing"); }//Create Pipeline $sPipePath = "My_pipe."    Posix_getpid ();    if (!posix_mkfifo ($sPipePath, 0666)) {die ("Create pipe {$sPipePath} error");            }//Simulation task concurrent for ($i = 0; $i < PC; + + $i) {$nPID = Pcntl_fork ();//create sub-process if ($nPID = = 0) { Subprocess Process//sleep (rand (1,TS));            Analog delay sleep (10);            $oW = fopen ($sPipePath, ' w '); Fwrite ($oW, $i. " \ n ");            The current task finishes processing, writing data fclose ($oW) in the pipeline; Exit (0);    Exit after execution}//parent process $oR = fopen ($sPipePath, ' R '); Stream_set_blocking ($oR, FALSE); Set the pipe to non-clogging to accommodate the timeout mechanism $sData = ';    Data in the storage pipeline $nLine = 0;    $nStart = time ();        while ($nLine < PC && (Time ()-$nStart) < to) {$sLine = Fread ($oR, 1024); if (empty ($sLine)) {COntinue;        } echo "Current line: {$sLine}\n";            Used to analyze how many tasks have been processed by ' \ n ' to identify foreach (Str_split ($sLine) as $c) {if ("\ n" = = $c) {+ + $nLine;    }} $sData. = $sLine;    } echo "Final line count: $nLine \ n";    Fclose ($oR); Unlink ($sPipePath);    Delete pipeline, has no effect//wait for the child process to complete, to avoid the zombie process $n = 0;        while ($n < PC) {$nStatus =-1;        $nPID = pcntl_wait ($nStatus, Wnohang);            if ($nPID > 0) {echo "{$nPID} exit\n";        + + $n;    }}//Verify the results, primarily to see if each task is completed $arr 2 = array ();          foreach (Explode ("\ n", $sData) as $i) {//Trim all if (is_numeric (Trim ($i))) {Array_push ($arr 2, $i);    }} $arr 2 = Array_unique ($arr 2);     if (count ($arr 2) = = PC) {echo ' OK '; } else {echo "error count". Count ($arr 2).        "\ n";    Var_dump ($arr 2);     }*/

The above is the code of their own test, basically did not learn anything valuable things.

NET pick code Source address: http://www.cnblogs.com/bourneli/archive/2012/07/06/2579893.html

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.