The pcntl_fork () function creates a child process that is only PID (process number) and ppid (parent process number) different from its parent process. For more information on how fork works in your system, check out the fork (2) manual for your system.
Note: PHP has a pcntl_fork function to implement multiple processes, but to load pcntl expansion, and only under Linux to compile this expansion.
1. First in the Ubuntu compiler pcntl.so, my Ubuntu can not find the PCNTL package, so create a folder to download the entire PHP package, found inside the PCNTL package to run the following command, the code is as follows:
| The code is as follows |
Copy Code |
# mkdir PHP # CD PHP # Apt-get Source PhP5 # CD php5-(whatever_release)/ext/pcntl # phpize #./configure (Note i) # make # make install Phpize command is used to prepare the PHP plug-in module of the compilation environment |
Successful installation will be built extname.so and placed in the PHP plug-in directory (preset stored in/usr/lib/php/modules/), need to adjust php.ini, join extension=extname.so After this line, you can use this plug-in module.
| The code is as follows |
Copy Code |
| void Pcntl_exec (String $path [, array $args [, array $envs]]) pcntl_exec-executes the specified program in the current process space, the code is as follows: $cmds =array ( Array ('/home/jerry/projects/www/test2.php '), Array ('/home/jerry/projects/www/test3.php ') );
foreach ($cmds as $cmd) { $pid =pcntl_fork (); if ($pid ==-1) { Process creation failed Echo ' returns 1 ' when creating a child process failure; Exit (-1); } else if ($pid) { The parent process will get the child process number, so this is the logic that the parent process executes Pcntl_wait ($status, Wnohang); } else{ Child process processing Logic Sleep (5); Pcntl_exec ('/usr/bin/php ', $cmd); Exit (0); } } |
example, examples of multiple pictures synchronized download, the code is as follows:
| The code is as follows |
Copy Code |
#!/usr/bin/php <?php page addresses that need to be crawled $url = ' http://www.jb51.net '; $content = file_get_contents ($url); Preg_match_all ('/echo "discovered". Count ($matches). Picture n ";
List ($SM, $ss) = Explode ("", Microtime ()); foreach ($matches as $k => $val) { $pid [$k] = Pcntl_fork (); if (! $pid [$k]) { Download ($url, $val); If a subprocess is to exit, a recursive multiple process is performed, and the parent process does not exit or terminate the multiple process Exit (0); }
if ($pid [$k]) { Pcntl_waitpid ($pid [$k], $status, wuntraced); }
} echo "Download complete n";
List ($em, $es) = Explode ("", Microtime ());
echo "When:" ($es + $em)-($ss + $sm), "n"; /** * Crawl Web Images * */ function Download ($url, $val) { $pic _url = $val [1]; if (Strpos ($val [1], '//')!== false) { ; } ElseIf (Preg_match) (' @^ (. *?) /@ ', $val [1], $inner _matches) = = 0) { $pic _url = $url. $val [1]; } ElseIf (Preg_match (' @[:.] @ ', $inner _matches[1], $tmp _matches) = = 0) { $pic _url = $url. $val [1]; }
$pic = file_get_contents ($pic _url);
if ($pic = = False) { Return }
Preg_match (' @/([^/]+) $@ ', $pic _url, $tmp _matches); You can use assert to handle exceptions $pic _file_name = $tmp _matches[1]; $f = fopen ("tmp/". $pic _file_name, "WB"); # Fwrite ($f, $pic); Fclose ($f); }
/* End of File pcntl_fork.php * * ?> |
Multi-process Sync download picture is a very useful technology, I hope this tutorial is helpful to you.