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.
Copy codeThe 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
}