PHP's pcntl process control pcntl_wait
This article mainly introduces pcntl_wait about PHP's pcntl process control. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it
pcntl_wait Introduction
# 来源官方 pcntl_wait — 等待或返回fork的子进程状态 int pcntl_wait ( int &$status [, int $options = 0 ] ) wait函数刮起当前进程的执行直到一个子进程退出或接收到一个信号要求中断当前进程或调用一个信号处理函数。 如果一个子进程在调用此函数时已经退出(俗称僵尸进程),此函数立刻返回。子进程使用的所有系统资源将 被释放。关于wait在您系统上工作的详细规范请查看您系统的wait(2)手册。 Note: 这个函数等同于以-1作为参数pid 的值并且没有options参数来调用pcntl_waitpid() 函数。 参数 status pcntl_wait()将会存储状态信息到status 参数上,这个通过status参数返回的状态信息可以用以下函数 pcntl_wifexited(), pcntl_wifstopped(), pcntl_wifsignaled(), pcntl_wexitstatus(), pcntl_wtermsig()以及 pcntl_wstopsig()获取其具体的值。 options 如果您的操作系统(多数BSD类系统)允许使用wait3,您可以提供可选的options 参数。如果这个参数没有提供,wait将会被用作系统调用。如果wait3不可用,提供参数 options不会有任何效果。options的值可以是0 或者以下两个常量或两个常量“或运算”结果(即两个常量代表意义都有效)。 options可用值 WNOHANG 如果没有子进程退出立刻返回。 WUNTRACED 子进程已经退出并且其状态未报告时返回。 返回值 pcntl_wait()返回退出的子进程进程号,发生错误时返回-1,如果提供了 WNOHANG作为option(wait3可用的系统)并且没有可用子进程时返回0。
Test code
<?php /** * Created by PhpStorm. * User: Object * Date: 2018/6/11 * Time: 10:28 */ if (strtolower(php_sapi_name()) != 'cli') { die("请在cli模式下运行"); } $index = 0; $loop = 1; while ($index < $loop) { echo "当前进程:" . getmypid() . PHP_EOL; $pid = pcntl_fork(); //fork出子进程 if ($pid == -1) { // 创建错误,返回-1 die('进程fork失败'); } else if ($pid) { // $pid > 0, 如果fork成功,返回子进程id // 父进程逻辑 pcntl_wait($status); // 父进程必须等待一个子进程退出后,再创建下一个子进程。 $child_id = $pid; //子进程的ID $pid = posix_getpid(); //获取当前进程Id $ppid = posix_getppid(); // 进程的父级ID $time = microtime(true); echo "我是父进程,fork的子进程id: {$child_id};当前进程id:{$pid};父进程id:{$ppid}; 当前index:{$index}; 当前时间:{$time}".PHP_EOL; } else { // $pid = 0 // 子进程逻辑 $cid = $pid; $pid = posix_getpid(); $ppid = posix_getppid(); $myid = getmypid(); $time = microtime(true); echo "我是子进程,当前进程id:{$pid};父进程id:{$ppid}; 当前index:{$index}; 当前时间:{$time}".PHP_EOL; //exit; //sleep(2); } $index++; }
loop = 1 execution result
当前进程:16604 我是子进程,当前进程id:16605;父进程id:16604; 当前index:0; 当前时间:1528696774.1978 我是父进程,fork的子进程id: 16605;当前进程id:16604;父进程id:15128; 当前index:0; 当前时间:1528696774.2032
loop = 2 execution result
当前进程:16613 我是子进程,当前进程id:16614;父进程id:16613; 当前index:0; 当前时间:1528696781.4751 当前进程:16614 我是子进程,当前进程id:16615;父进程id:16614; 当前index:1; 当前时间:1528696781.4756 我是父进程,fork的子进程id: 16615;当前进程id:16614;父进程id:16613; 当前index:1; 当前时间:1528696781.4802 我是父进程,fork的子进程id: 16614;当前进程id:16613;父进程id:15128; 当前index:0; 当前时间:1528696781.4858 当前进程:16613 我是子进程,当前进程id:16616;父进程id:16613; 当前index:1; 当前时间:1528696781.4863 我是父进程,fork的子进程id: 16616;当前进程id:16613;父进程id:15128; 当前index:1; 当前时间:1528696781.4913
loop = 3 execution result
当前进程:16625 我是子进程,当前进程id:16626;父进程id:16625; 当前index:0; 当前时间:1528696787.3334 当前进程:16626 我是子进程,当前进程id:16627;父进程id:16626; 当前index:1; 当前时间:1528696787.3338 当前进程:16627 我是子进程,当前进程id:16628;父进程id:16627; 当前index:2; 当前时间:1528696787.3345 我是父进程,fork的子进程id: 16628;当前进程id:16627;父进程id:16626; 当前index:2; 当前时间:1528696787.3391 我是父进程,fork的子进程id: 16627;当前进程id:16626;父进程id:16625; 当前index:1; 当前时间:1528696787.3434 当前进程:16626 我是子进程,当前进程id:16629;父进程id:16626; 当前index:2; 当前时间:1528696787.3441 我是父进程,fork的子进程id: 16629;当前进程id:16626;父进程id:16625; 当前index:2; 当前时间:1528696787.3496 我是父进程,fork的子进程id: 16626;当前进程id:16625;父进程id:15128; 当前index:0; 当前时间:1528696787.3543 当前进程:16625 我是子进程,当前进程id:16630;父进程id:16625; 当前index:1; 当前时间:1528696787.3548 当前进程:16630 我是子进程,当前进程id:16631;父进程id:16630; 当前index:2; 当前时间:1528696787.3555 我是父进程,fork的子进程id: 16631;当前进程id:16630;父进程id:16625; 当前index:2; 当前时间:1528696787.3599 我是父进程,fork的子进程id: 16630;当前进程id:16625;父进程id:15128; 当前index:1; 当前时间:1528696787.3643 当前进程:16625 我是子进程,当前进程id:16632;父进程id:16625; 当前index:2; 当前时间:1528696787.3649 我是父进程,fork的子进程id: 16632;当前进程id:16625;父进程id:15128; 当前index:2; 当前时间:1528696787.3697
Summary
1. From the results of multiple executions, it is known that the program creates fork from outside to inside. Then exit from the last fork
2. For example, after a fork, the parent process of the program is blocked due to pcntl_wait, and then waits for the child process of this fork to exit, and then the parent process of the corresponding child process executes the logic and exits
3. Then execute the parent process of this child process in order to exit the logic of loop 2, and finally end the total process
The above is the entire content of this article, I hope it will be helpful to everyone's learning, please pay attention to more related content PHP Chinese website!
Related recommendations:
The above is the detailed content of PHP's pcntl process control pcntl_wait. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
