PHP Multi-process implementation

Source: Internet
Author: User

PHP Multi-process implementation

PHP has a set of process control functions (–ENABLE-PCNTL and POSIX extensions are required at compile time), which enables PHP to create sub-processes like C in the Nginx system, execute programs using the EXEC function, and process signals.

CentOS 6 under the Yum install PHP, the default is not to install PCNTL, so you need to compile the installation separately, first download the corresponding version of PHP, after decompression

[Plain]View Plaincopy print?
    1. CD Php-version/ext/pcntl
    2. Phpize
    3. ./configure && make && make install
    4. Cp/usr/lib/php/modules/pcntl.so/usr/lib64/php/modules/pcntl.so
    5. echo "extension=pcntl.so" >>/etc/php.ini
    6. /ETC/INIT.D/HTTPD restart

It's very convenient.

Here is the sample code:

[PHP]View Plaincopy print?
    1. <?php
    2. Header (' Content-type:text/html;charset=utf-8 ');
    3. Extension must be loaded
    4. if (!function_exists ("Pcntl_fork")) {
    5. Die ("Pcntl extention is must!");
    6. }
    7. The number of total processes
    8. $totals = 3;
    9. Number of scripts executed
    10. $CMDARR = Array ();
    11. An array of the number of scripts executed
    12. for ($i = 0; $i < $totals; $i + +) {  
    13. $CMDARR [] = Array ("path" = = __dir__.  "/run.php", ' pid ' =$i,' total ' =$totals);
    14. }
    15. /*
    16. Expand: $CMDARR
    17. Array
    18. (
    19. [0] = = Array
    20. (
    21. [Path] =/var/www/html/company/pcntl/run.php
    22. [PID] = 0
    23. [Total] = 3
    24. )
    25. [1] = = Array
    26. (
    27. [Path] =/var/www/html/company/pcntl/run.php
    28. [PID] = 1
    29. [Total] = 3
    30. )
    31. [2] = = Array
    32. (
    33. [Path] =/var/www/html/company/pcntl/run.php
    34. [PID] = 2
    35. [Total] = 3
    36. )
    37. )
    38. */
    39. Pcntl_signal (SIGCHLD, sig_ign);  //If the parent process does not care what time the child process ends, the kernel is reclaimed after the child process is finished.
    40. foreach ($cmdArr as $cmd) {
    41. $pid = Pcntl_fork (); //Create child process
    42. //parent and child processes will execute the following code
    43. if ($pid = =-1) {
    44. //Error handling: Returns 1 when creating a child process failure.
    45. Die (' could not fork ');
    46. } Else if ($pid) {
    47. //Parent process will get the child process number, so here is the logic that the parent process executes
    48. //If you do not need to block the process and want the exit status of the child process, you can comment out the pcntl_wait ($status) statement, or write:
    49. Pcntl_wait ($status, Wnohang);  //Wait for the child process to break to prevent the child process from becoming a zombie process.
    50. } Else {
    51. //Sub-process gets a $pid of 0, so here is the logic that the child process executes.
    52. $path = $cmd ["path"];
    53. $pid = $cmd [' pid '];
    54. $total = $cmd [' Total '];
    55. echo exec ("/usr/bin/php {$path} {$pid} {$total}")."  \ n ";
    56. exit (0);
    57. }
    58. }
    59. ?>

PHP Multi-process implementation

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.