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