<?php
$pid = Pcntl_fork ();
if ($pid = =-1)
{
Die ("Could not fork");
}
ElseIf ($pid = = 0)
{
echo "I ' m the childProcess\ n ";
}
Else
{
echo "I ' m The parentProcess\ n ";
Exit
}
to understand the fork execution process, you must first understand the operating system "process" of the concept.
A process that consists mainly of three elements:
1. A procedure that can be implemented;
2. All data associated with the process (including variables, memory space, buffers, etc.);
3. The execution context of the program (execution context);
It is simple to understand that a process represents a state in the execution of an executable program. Operating system management of processes, typically, is done through the process table. Each table entry in the process table that records the situation of a process in the current operating system. In the case of a single CPU, only one process consumes the CPU at each particular time, but there may be multiple active (pending or continued) processes in the system.
A register called "program counter, PC," which indicates the position of the next instruction to be executed by the current CPU-consuming process.
When the CPU time allocated to a process is exhausted, the operating system saves the value of the register associated with the process to the corresponding table entry in the process table; The context of the process that will take over the CPU of the process, read from the process table, and update the corresponding register (this process is called "Context Exchange" ( Process context Switch) ", the actual contextual exchange needs to involve more data, which is not related to fork, no longer say, the main thing to remember is that the program register PC indicates where the program is currently executing, is the important content of the process context, swapped out CPU process to save the value of this register, the process of swapping into the CPU, but also according to the process table to save the process of executing context information, update this register.
Well, there are these concepts to hit bottom, which can be said fork out when your program executes to the following statement:
PID = Pcntl_fork ();
The operating system creates a new process (child process), and establishes a new table entry for it in the process table accordingly. The new process and the executable procedure of the original process are the same program; The majority of the context and data is the copy of the original process (parent process), but they are two separate processes! At this point the program register PC in the context of the parent, child process claims that the process is currently executing to the fork call is about to return (at this time the child process does not occupy the CPU, the child process of the PC is not really saved in the register, but as the process context is saved in the process table in the corresponding table key). The question is how to go back and split up in the parent-child process.
The parent process continues the implementation of the fork in the operating system so that the call returns the PID (a positive integer) of the child process that was just created in the parent process, so that the two branches of pid==0 are not executed in the subsequent if statement pid<0. So the output: I am the parent process ...
then the child process is dispatched at some later time, its context is swapped in, the CPU is occupied, and the implementation of the fork in the operating system causes the fork call in the sub-process to return 0, so in this process Zhong Pid=0 (Note that this is not the parent process, although it is the same program, but this is another execution of the same program, in the operating system this execution is represented by another process, from the perspective of execution and the parent process is independent of each other). While this process continues, the IF statement does not satisfy the pid<0, but Pid==0 is true, so the output: I am the child process ...
I think you are more confused is:
Why it seems that the two branches of the program are all mutually exclusive, which is of course not possible in one execution of a program, in fact the two lines of output you see are from two separate processes, and the two processes are executed two times from the same program.
-----------------------------------------
After 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 in the operating system it appears that they are more like siblings, that the 2 processes share the code space, but that the data space is independent and that the content in the child process data space is a complete copy of the parent process. Instruction pointers are exactly the same, but only a little different, if Fork succeeds, 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, and if the fork fails, the parent process returns an error.
It can be imagined that 2 processes have been running at the same time, and Unison, after the fork, they do different work, that is, bifurcation, which is why fork is called fork reason.
As to which process is running first, this is related to the scheduling algorithm of the operating system platform, and this problem is not important in practical application, if the parent-child process is needed to work together, it can be solved by controlling the grammatical structure.
-----------------------------------------
Fork before child process can inherit something about the parent process , but in pcntl_fork () There is no inheritance between the post-child process and the parent process. What is created in a child process is a child process, and what is created in the parent process is the parent process, which can be considered entirely as two separate processes.
-----------------------------------------
It was used in the program section. pcntl_fork () after the program out of bifurcation, derived from two processes, the specific which run first to see the system scheduling algorithm.
Here we can think so, in running to "pid=pcntl_fork ();" The system derives a sub-process that is identical to the main program. The process of "pid=pcntl_fork ();" In a sentence, the PID is the PID of the child process itself; After the child process is finished, the parent process "pid=pcntl_fork ();" The PID in the parent process itself, so the program has two lines of output.
-----------------------------------------
pcntl_fork () The function copies the PCB of the current process, and returns the PID of the derived child process to the parent process, the father-child process is parallel, the print statement has a full view of the system scheduling algorithm, the content control of the print is controlled by the PID variable. Because we know that pcntl_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 subprocess is not changed, which makes us see their different outputs.
-----------------------------------------
1. The process of deriving a child process, that is, the parent process, whose PID is unchanged;
2. For a sub-process, the fork () function returns 0, but its own PID is definitely not 0, and the fork () function returns 0 to it because it can call Getpid () at any time to obtain its own PID;
3. After fork the parent and child processes are not sure who will run first, or who ends first, unless 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.
-----------------------------------------
Php-pcntl_fork () execution process