An example of PHP multi-process implementation code

WBOY
Release: 2016-07-25 08:58:32
Original
935 people have browsed it
  1. //while(1)//Loop through 3 processes
  2. //{
  3. //declare(ticks=1);
  4. $bWaitFlag = FALSE; // Whether to wait for the process to end
  5. //$bWaitFlag = TRUE; // Whether to wait for the process to end
  6. $intNum = 3; // Total number of processes
  7. $pids = array(); // Process PID array
  8. for($i = 0; $i <$intNum ; $i++){
  9. // Generate a child process, and start the test run code from the current line, and do not inherit the data information of the parent process
  10. $pids[$i] = pcntl_fork();
  11. /*if($pids [$i])//Parent process
  12. {
  13. //echo $pids[$i]."parent"."$i -> " . time(). "n";
  14. }
  15. */
  16. if( $pids[$i] == -1){
  17. echo "couldn't fork". "n";
  18. }elseif(!$pids[$i]){
  19. sleep(1);
  20. echo "n". "The ".$i."th process-> " . time(). "n";
  21. //$url=" http://xxx/comments.php?p=".$i;//Catch Take the example of the page
  22. //$content = file_get_contents($url);
  23. //file_put_contents('message.txt',$content);
  24. //echo "n"."th ".$i." process - > " ."Capture page".$i."-> " . time()."n";
  25. exit(0);//The child process must exit, otherwise recursive multi-processing will occur. The parent process must not exit. Otherwise, terminate the multi-process
  26. }
  27. if ($bWaitFlag){
  28. pcntl_waitpid($pids[$i], $status, WUNTRACED);
  29. echo "wait $i -> " . time() . "n";
  30. }
  31. }
  32. //sleep(1);
  33. } //by bbs.it-home.org
  34. ?>
Copy code

fork: The operating system will copy a child process that is exactly the same as the parent process, Although it is a father-son relationship, from the perspective of the operating system, they are more like a brotherly relationship. The two processes share the code space, but the data spaces are independent of each other. The content of the child process's data space is a complete copy of the parent process, and the instruction pointer Also exactly the same. But there is only one difference. If fork is successful, the return value of fork in the child process is 0. The return value of fork in the parent process is the process number of the child process. If fork is unsuccessful, the parent process will return an error,

The two processes have been running at the same time and in the same pace. After the fork, they do different tasks, that is, they bifurcate. This is why fork is called fork.

As for which one runs first, it may be related to the operating system, and this problem is not important in actual applications. If parent-child process coordination is required, it can be solved through primitives.

The child process can inherit the things of the parent process before fork, but after fork, the child process has no inheritance relationship with the parent process. Things created in the child process belong to the child process, and things created in the parent process belong to the parent process. It can be completely regarded as two processes.

After using fork(); in the program segment, the program bifurcated and spawned two processes. Which one runs first depends on the scheduling algorithm of the system.

It can be thought that when running to "pid=fork();", the system spawns a sub-process that is exactly the same as the main program. The pid in the "pid=fork();" sentence of the process is the pid of the child process itself; after the child process ends, the pid in the "pid=fork();" of the parent process is the pid of the parent process itself. Therefore, the modified program has two lines of output.

The

fork() function copies the PCB of the current process and returns the pid of the derived child process to the parent process. Moreover, according to the tip of "corand" brother above, the father and son processes are parallel, and the order of printing statements completely depends on the system's scheduling algorithm. The printed content is controlled by the pid variable. Because we know that fork() returns the pid of the derived child process to the parent process, which is a positive integer; and the pid variable of the derived child process has not been changed. This difference allows us to see their different outputs.

1. The process that derives the child process, that is, the parent process, has the same pid;

2. For the child process, fork returns 0 to it, but its pid will never be 0; the reason why fork returns 0 to it is because it can call getpid() at any time to get its own pid;

3. Unless synchronization is used for the master process after the fork, it is impossible to determine who runs first or who ends first. It is wrong to think that the parent process returns from fork only after the child process ends. This is not the case with fork, but with vfork.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template