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
The process that derives the child process, that is, the parent process, has the same pid;
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;
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";
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!

Python中的支持向量机(SupportVectorMachine,SVM)是一个强大的有监督学习算法,可以用来解决分类和回归问题。SVM在处理高维度数据和非线性问题的时候表现出色,被广泛地应用于数据挖掘、图像分类、文本分类、生物信息学等领域。在本文中,我们将介绍在Python中使用SVM进行分类的实例。我们将使用scikit-learn库中的SVM模

随着新一代前端框架的不断涌现,VUE3作为一个快速、灵活、易上手的前端框架备受热爱。接下来,我们就来一起学习VUE3的基础知识,制作一个简单的视频播放器。一、安装VUE3首先,我们需要在本地安装VUE3。打开命令行工具,执行以下命令:npminstallvue@next接着,新建一个HTML文件,引入VUE3:<!doctypehtml>

VAE是一种生成模型,全称是VariationalAutoencoder,中文译作变分自编码器。它是一种无监督的学习算法,可以用来生成新的数据,比如图像、音频、文本等。与普通的自编码器相比,VAE更加灵活和强大,能够生成更加复杂和真实的数据。Python是目前使用最广泛的编程语言之一,也是深度学习的主要工具之一。在Python中,有许多优秀的机器学习和深度

随着互联网的普及,验证码已经成为了登录、注册、找回密码等操作的必要流程。在Gin框架中,实现验证码功能也变得异常简单。本文将介绍如何在Gin框架中使用第三方库实现验证码功能,并提供示例代码供读者参考。一、安装依赖库在使用验证码之前,我们需要安装一个第三方库goCaptcha。安装goCaptcha可以使用goget命令:$goget-ugithub

随着互联网的迅速发展,数据已成为了当今信息时代最为重要的资源之一。而网络爬虫作为一种自动化获取和处理网络数据的技术,正越来越受到人们的关注和应用。本文将介绍如何使用PHP开发一个简单的网络爬虫,并实现自动化获取网络数据的功能。一、网络爬虫概述网络爬虫是一种自动化获取和处理网络资源的技术,其主要工作过程是模拟浏览器行为,自动访问指定的URL地址并提取所

PHPPCNTL中fork失败的常见错误及解决方案在使用PHPPCNTL扩展进行进程管理时,经常会遇到fork失败的问题。fork是创建子进程的一种方法,在一些情况下可能会因为一些错误导致fork操作失败。本文将介绍一些常见的fork失败的错误以及相应的解决方案,并提供具体的代码示例来帮助读者更好地理解和处理这些问题。1.内存不足可能的错误信息:Can

生成对抗网络(GAN,GenerativeAdversarialNetworks)是一种深度学习算法,它通过两个神经网络互相竞争的方式来生成新的数据。GAN被广泛用于图像、音频、文字等领域的生成任务。在本文中,我们将使用Python编写一个GAN算法实例,用于生成手写数字图像。数据集准备我们将使用MNIST数据集作为我们的训练数据集。MNIST数据集包含

Lasso回归是一种流行应用于机器学习的线性回归方法,目的是通过忽略不相关的特征变量来寻找最佳拟合模型。本文将介绍如何在Python中实现Lasso回归,并提供一个实际的数据集进行演示。Lasso回归简介Lasso回归是一种通过向目标函数中添加惩罚项来解决普通最小二乘问题的方法。该惩罚项利用L1正则化(也称为Lasso惩罚)来实现,其形式如下所示:$J(e


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.