A case of PHP multi-process implementation code

Source: Internet
Author: User
  1. while (1)//loop takes 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. Produces a child process and runs code from below the current line without inheriting 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". " Section ". $i." Process, ". Time (). "\ n";
  21. $url = "http://xxx/comments.php?p=". $i;//Example of crawling pages
  22. $content = file_get_contents ($url);
  23. File_put_contents (' Message.txt ', $content);
  24. echo "\ n". " Section ". $i." Process, ". Crawl page ". $i." ". Time (). " \ n ";
  25. Exit (0);//child process to exit otherwise recursive multi-process, parent process do not exit otherwise terminate 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 replicates a child process that is exactly the same as the parent process, although it is a parent-child relationship, but it seems to the operating system that they are more brotherly, that the two processes share the code space, but that the data space is independent, and that the content of the child process data space is the full copy of the parent process, and the instruction But only with a little difference, if Fork succeeds, the return value of the child process fork is 0 The return value of fork in the parent process is the process number of the child process, if the fork is unsuccessful, the parent process returns the error,

2 processes are running at the same time, and Unison, after the fork, they do different jobs, that is, bifurcation. This is the reason why fork is called fork.

As for the first run, may be related to the operating system, and this problem is not important in practical applications, if the need for parent-child process synergy, can be resolved through the primitive language.

Fork the pre-parent process can inherit, and the child process does not have any inheritance relationship with the parent process after the fork. What is created in a child process is a child process, and what is created in the parent process is the parent process. Can be viewed entirely as two processes.

Fork () was used in the program section, and then the program was forked, deriving two processes. The specific which runs first looks at the system's scheduling algorithm.

Can think so, in running to "pid=fork ();" The system derives a sub-process that is identical to the main program. The process of "pid=fork ();" In a sentence, the PID is the PID of the child process itself; After the child process is finished, the parent process "pid=fork ();" The PID of the parent process itself. Therefore, the 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. And according to the above "Corand" elder brother's hint, father and son process parallel, print the statement of the order completely look at the system scheduling algorithm. The content control of the print 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 for the derived subprocess is not changed. This distinction allows us to see their different outputs.

1, the process of deriving a child process, that is, the parent process, its PID is unchanged;

2, the sub-process, fork returned to it 0, but its PID is definitely not 0, the reason that fork returns 0 to it, because it can call Getpid () at any time to obtain their own PID;

3,fork the master process will not be able to determine who is running first or who will end first unless the synchronization is used. It is not right to think that the child process is over stepfather process is returned from fork, which is not the case with fork, vfork.

  • 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.