Passage 2: using pcntl_ Fock function talking about multi process

Time:2021-7-19

introduce

pcntlyesphpOfProcess control extension, seePHP manual。 amongpcntl_fork()Is to create a process function.

Workman uses this extension

ps -ef | grepperhapspstree -apn | grepIt must have been used in general, and the printed one is like this:

Passage 2: using pcntl_ Fock function talking about multi process

One in the picture abovemasterThe following two branches are the child process.

Those who have used workman are also familiar with the above structure. The bottom layer of workman is written in PHP, which depends on PHP extensionpcntlIt is one of the most important dependency extensionsmaster workerThe structure is implemented by this extension.

Official demo

Back to the point, usepcntl_fork()Make it happen,forkFork a subprocess, that is, to create a subprocess.

<? php

Principle analysis

The above code is the official sample code, and the specific process is to execute topcntl_fork()Copy as like as two peas.

Passage 2: using pcntl_ Fock function talking about multi process

in other wordspcntl_fork();At the same time, the following code is executed in both processes (including$pidAssignment), so$pidThe assignments in the two processes are different, and they are returned in the parent process$pidGreater than 0, equal to the process of the child processidIt’s usually calledpid(process id)。 And the$pidIs 0.

So throughifThe logic of which role should be executed in which code block is distinguished by judgment.master-workerModel, generalmasterIt is responsible for managing the number of subprocesses, processing signals and so on. It doesn’t process business logic, butworkerProcess, as the name suggests, work process.

<? php

A process structure similar to nginx has emerged

Passage 2: using pcntl_ Fock function talking about multi process

Passage 2: using pcntl_ Fock function talking about multi process

pcntl_ The role of wait

pcntl_wait($status);It is a function to prevent a child process from becoming a zombie process. It is blocked, similar to listening. That is, when the subprocess exits after execution, or activelyexit()The parent process assigns the capture status code to the$statusAnd clean up the child processes.

The parent and child processes are isolated

The last point, mentioned earlier: execution topcntl_fork()A copy of the original document is “copied” at the same time. Note that the child process inherits the variables and methods of the parent process, but because it is two processes (two scripts), the reassignment of variables in the child process will not affect the value of variables in the parent process, and the processes are isolated.

<? php

Execution

Passage 2: using pcntl_ Fock function talking about multi process

You can see it printed first1Is the output of the parent process, followed by2Is the output of the child process.

This work adoptsCC agreementReprint must indicate the author and the link of this article

focus

Recommended Today

Drawing 3D Earth by webgl

It may be convenient to display the 3D model through three.js, but do you know how it works step by step from mesh construction to mapping to final rendering? Now we can implement it directly by using the underlying web GL and a little mathematical knowledge. The effect of this section:Webgl 3D Earth content syllabus […]