Php parent-child process Communication

Source: Internet
Author: User
The multi-process demo describes how to fork sub-processes. However, if you have any children, you must remember to handle them. Otherwise, you may be confused one day, right. There are many communication methods between processes. Here we use the simplest signal communication. When we kill-9 a process, we actually send a termination signal to the process.

The multi-process demo describes how to fork sub-processes. However, if you have any children, you must remember to handle them. Otherwise, you may be confused one day, right. There are many communication methods between processes. Here we use the simplest signal communication. When we kill-9 a process, we actually send a termination signal to the process.

The multi-process demo describes how to fork sub-processes.
However, if you have any children, you must remember to handle them. Otherwise, you may be confused one day, right.

There are many communication methods between processes. Here we use the simplest signal communication. When we kill-9 a process, we actually send a termination signal to the process, which has already been bound to a signal processor. Therefore, we can use other processes to notify another process to respond accordingly (kill-9 will quit ). Parent-Child Process Communication is no exception. The parent process is bound with a signal processor to coordinate child processes. The child process sends signals to the parent process so that the parent process can respond accordingly.

Signal binding processor: pcntl_signal http://cn2.php.net/manual/en/function.pcntl-signal.php#70161
Send signal: posix_kill http://cn2.php.net/manual/en/function.posix-kill.php
Note that the signal cannot be reentrant. When the signal processor of a sub-process is processing, other signal processing will be ignored. Therefore, pcntl_watipid is required.
The http://cn2.php.net/manual/en/function.pcntl-waitpid.php to wait.

Using the above theory, we have implemented a fixed sub-process processing task program:
Parent process fork sub-process, and then bind the SIGCHLD signal. The sub-process ends and triggers the SIGCHLD signal processor of the parent process. At the end of the main loop, count the number of sub-processes. When the value is lower than the specified value, fork's new sub-process: the code is as follows:

  0 ) {         if($signal == SIGCHLD){            $child_nums--;          }       }   }function fork_do($c){    global $child_nums;    while($c--) {        if(pcntl_fork() == 0 ) {             real_do();                      } else {            $child_nums++;          }       }   }function real_do(){    sleep(rand(1,3));    exit();}$manage_pid = getmypid();$fork_nums = 4;pcntl_signal(SIGCHLD,'catch_signal');fork_do($fork_nums);while($child_nums != 0){     echo "Childs " . $child_nums . "\n";        if($child_nums < $fork_nums) {        fork_do($fork_nums - $child_nums);      }   }         

Note: If the dynamic effect looks cool, change sleep to usleep or simply remove it.

Original article address: php parent-child process communication, thanks to the original author for sharing.

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.