Detailed explanation of pcntl_fork execution process example

小云云
Release: 2023-03-20 22:16:02
Original
2000 people have browsed it

This article mainly shares with you an example of the pcntl_fork execution process. The concept of process (process) is a process, which mainly contains three elements:

1. An executable program;
2. All data associated with the process (including variables, memory space, buffers, etc.);
3. The execution context of the program;

can be thought of like this

It may be simply understood that a process represents an execution process of an executable program a state in. The operating system's management of processes is typically accomplished through the process table. Each entry in the process table records the status of a process in the current operating system.
For the case of a single CPU, only one process occupies the CPU at a specific time, but there may be multiple active (waiting for execution or continued execution) processes in the system at the same time. A register called the program counter (pc) indicates the location of the next instruction to be executed by the process currently occupying the CPU. When the CPU time allocated to a process has been used up, the operating system saves the value of the register related to the process to the corresponding entry of the process in the process table; the context of the process that will take over the CPU from this process is Read from the process table and update the corresponding register (this process is called "Context switch (process context switch)". The actual context switch needs to involve more data, which has nothing to do with fork. No more details. The main thing to remember is that the program register pc indicates where the program has been executed. It is an important part of the process context. The process that is swapped out of the CPU must save the value of this register. The process that is swapped into the CPU must also be based on the process table. Update this register with the execution context information of this process saved in it).

fock knowledge

After 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 brothers Relationship, these two processes share code space, but the data space is independent of each other. The content in the child process's data space is a complete copy of the parent process, and the instruction pointer is exactly the same, but there is only one difference. If the fork succeeds, the child process's data space is a complete copy of the parent process. The return value of fork is 0. The return value of fork in the parent process is the process number of the child process. If fork fails, the parent process will return an error. It can be imagined that the two processes have been running at the same time and in the same pace. After the fork, they do different tasks respectively, that is, they bifurcate. This is why fork is called fork. As for which process runs first, this is related to the scheduling algorithm of the operating system platform, and this problem is not important in practical applications. If parent and child processes need to work together, it can be solved by controlling the syntax structure.

2

The child process can inherit the things of the parent process before fork, but after pcntl_fork(), the child process and the parent process have nothing. Inheritance relationship. Things created in the child process belong to the child process, and things created in the parent process belong to the parent process. They can be completely regarded as two independent processes.

3

After using pcntl_fork() in the program segment, the program bifurcated and two processes were derived. Which one is specific? The first thing to run depends on the scheduling algorithm of the system.
Here, we can think that when running to "pid=pcntl_fork();", the system spawns a subprocess that is exactly the same as the main program. The pid obtained in the "pid=pcntl_fork();" sentence of the process is the pid of the child process itself; after the child process ends, the pid obtained in the "pid=pcntl_fork();" sentence of the parent process is the pid of the parent process itself. So the program has two lines of output.

4

pcntl_fork() function copies the PCB of the current process and returns the pid of the derived child process to the parent process. The parent and child processes In parallel, the order of printing statements depends entirely on the scheduling algorithm of the system, and the content of printing 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; while the pid variable of the derived child process has not been changed, this difference allows us to see their different outputs.

5

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

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

  3. After fork, unless synchronization is used between the parent and child processes, 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.

Example

<?php$lock = new swoole_lock(SWOOLE_MUTEX);echo "[主进程]create lock\n";$lock->lock();$res = pcntl_fork();if ($res>0)
{    echo "1\n";    $lock->unlock();
    sleep(1);    echo "222";
} 
else{    echo "[子进程] Wait Lock\n";    $lock->lock();    echo "[子进程] Get Lock\n";    $lock->unlock();    exit("[子进程] exit\n");
}echo "[主进程]release lock\n";unset($lock);echo "[主进程]exit\n";
Copy after login

Detailed explanation of pcntl_fork execution process example

Related recommendations:

Weibo Qzone WeChat pcntl_fork implements PHP multi-process

Detailed introduction to pcntl_fork in php multi-process

php Usage of pcntl_fork and pcntl_fork

The above is the detailed content of Detailed explanation of pcntl_fork execution process example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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