This is a multi-thread method for two php programs. it is unavailable in Windows and needs to be compiled in Linux. The pcntl_fork () function is used to create a sub-process for the current process. Run the parent process first, and the PID of the child process is returned. it must be greater than zero. In the code of the parent process, you can use pcntl_fork (& $ status) to pause the parent process to know that its child process has returned values. Note: blocking of the parent process also blocks the child process. However, the completion of the parent process does not affect the running of the child process.
After the parent process is run, the child process is run. The child process starts to execute (including this function) from the statement that executes pcntl_fork ), however, it returns zero (representing a sub-process ). It is better to have an exit statement in the code block of the child process, that is, the execution of the child process ends immediately after the execution. Otherwise, it will re-execute some parts of the script (the rule has not been summarized ).
Note the following two points:
1. It is best for a sub-process to have an exit statement to prevent unnecessary errors;
2.
The code is as follows:
$ Pid = pcntl_fork ();
// It is better not to have other statements here
If ($ pid =-1 ){
Die ('could not fork ');
} Else if ($ pid ){
// We are the parent
Pcntl_wait ($ status); // Protect against Zombie children
} Else {
// We are the child
}
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