- while (1)//loop takes 3 processes
- //{
- Declare (Ticks=1);
- $bWaitFlag = FALSE; Whether to wait for the process to end
- $bWaitFlag = TRUE; Whether to wait for the process to end
- $intNum = 3; Total number of processes
- $pids = Array (); Process PID Array
- for ($i = 0; $i < $intNum; $i + +) {
- Produces a child process and runs code from below the current line without inheriting the data information of the parent process
- $pids [$i] = Pcntl_fork ();
- /*if ($pids [$i])//Parent process
- {
- echo $pids [$i]. " Parent "." $i. " Time (). "\ n";
- }
- */
- if ($pids [$i] = =-1) {
- echo "couldn ' t fork". "\ n";
- }elseif (! $pids [$i]) {
- Sleep (1);
- echo "\ n". " Section ". $i." Process, ". Time (). "\ n";
- $url = "http://xxx/comments.php?p=". $i;//Example of crawling pages
- $content = file_get_contents ($url);
- File_put_contents (' Message.txt ', $content);
- echo "\ n". " Section ". $i." Process, ". Crawl page ". $i." ". Time (). " \ n ";
- Exit (0);//child process to exit otherwise recursive multi-process, parent process do not exit otherwise terminate multi-process
- }
- if ($bWaitFlag) {
- Pcntl_waitpid ($pids [$i], $status, wuntraced);
- echo "Wait $i." Time (). "\ n";
- }
- }
- Sleep (1);
- }//by bbs.it-home.org
- ?>
Copy CodeFork: 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. |