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. Thefork() 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. |