PHP Multi-process Pcntl learning

Source: Internet
Author: User

Pcntl is not available under Windows, Linux compiles php with parameter--enable-pcntl. The first use of the Pcntl module, encountered some pits are slowly filled in, here simply recorded.

1. Variables cannot be shared between child processes.

2. Child process complete, remember to add exit (), the child process exits, otherwise there will be pits.

1<?PHP2 $pid _dir= __dir__. ' /pid_files/';3 4  for($i= 0;$i<3;$i++){5     $pid=pcntl_fork ();6     if($pid= =-1){7         Var_dump(' Fork failed ');8     }9 Ten     if($pid= = 0){ One         //the child Process code that returns the current process ID A         $pid=Posix_getgid (); -         //returns the current parent process identity -         $ppid=posix_getppid (); the         $r=Rand(0,100);//Random number - //Create a file -         file_put_contents($pid _dir." fork_child_process_{$i}_{$ppid}_{$pid}_{$r}",$e); -         //Note that the child process is complete, remember to add exit (), the child process exits, or there will be pits + //reference: 1190000003503671 -         Exit(); +     } A } at  -  - $pid=posix_getpid (); - $ppid=posix_getppid (); - $r=Rand(0,100);//Random number - Touch("$pid _dir/fork_process_pid_{$ppid}_{$pid}_$r");

  

  

PHP Multi-process Pcntl learning

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.