PHP's pcntl process control pcntl_fork

不言
Release: 2023-04-02 18:48:01
Original
2424 people have browsed it

This article mainly introduces pcntl_fork about PHP's pcntl process control. It has a certain reference value. Now I share it with you. Friends in need can refer to it

pcntl Introduction

PHP's process control support implements Unix-style process creation, program execution, signal processing, and process interruption. Process control cannot be used in a Web server environment and may cause unexpected results when used in a Web serving environment.

This document is used to explain the common usage of each process control function. For more information about Unix process control, it is recommended that you consult the system documentation about fork(2), waitpid(2), signal(2), etc. or more comprehensive reference materials such as "Advanced Programming in Unix Environment" (author: W. Richard Stevens, Addison-Wesley).

PCNTL now uses ticks as the callback mechanism for signal processing. The speed of ticks far exceeds the previous processing mechanism. This change follows the same semantics as "user ticks". You can use the declare() statement to specify where callbacks are allowed to occur in your program. This allows us to minimize the overhead of asynchronous event handling. Enabling pcntl when compiling PHP will always incur this overhead, regardless of whether pcntl is actually used in your script.

One tweak is that all pcntl scripts prior to PHP 4.3.0 to make it work, either use declare() in the section (of code) where callbacks are expected to be allowed, or use the new global syntax of declare() to make it work Valid throughout the entire script.

Note: This extension is not available on Windows platforms.

Official document

pcntl official document

pcntl_fork

# 来源官方

PHP 4 >= 4.1.0, PHP 5, PHP 7)

pcntl_fork — 在当前进程当前位置产生分支(子进程)。译注:fork是创建了一个子进程,父进程和子进程 都从fork的位置开始向下继续执行,不同的是父进程执行过程中,得到的fork返回值为子进程 号,而子进程得到的是0。

说明
int pcntl_fork ( void )
pcntl_fork()函数创建一个子进程,这个子进程仅PID(进程号) 和PPID(父进程号)与其父进程不同。fork怎样在您的系统工作的详细信息请查阅您的系统 的fork(2)手册。

返回值
成功时,在父进程执行线程内返回产生的子进程的PID,在子进程执行线程内返回0。失败时,在 父进程上下文返回-1,不会创建子进程,并且会引发一个PHP错误。
Copy after login

Code

<?php
/**
 * Created by PhpStorm.
 * User: Object
 * Date: 2018/6/11
 * Time: 10:12
 */

const NEWLINE = "\n\n";

if (strtolower(php_sapi_name()) != &#39;cli&#39;) {
    die("请在cli模式下运行");
}

echo "当前进程:" . getmypid() . NEWLINE;

$pid = pcntl_fork(); //fork出子进程

//fork后父进程会走自己的逻辑,子进程从处开始走自己的逻辑,堆栈信息会完全复制给子进程内存空间,父子进程相互独立

if ($pid == -1) { // 创建错误,返回-1

    die(&#39;进程fork失败&#39;);

} else if ($pid) { // $pid > 0, 如果fork成功,返回子进程id

    // 父进程逻辑
    $time = microtime(true);
    echo "我是父进程:{$time}".NEWLINE;

} else { // $pid = 0

    // 子进程逻辑
    $time = microtime(true);
    echo "我是子进程:{$time}".NEWLINE;
}
Copy after login

Execution result

当前进程:17472

我是父进程:1528697500.2961

我是子进程:1528697500.2961
Copy after login
Copy after login

after fork Will the child process execute the logic first or the parent process execute the logic first?

Test code

Here we change the if order of the parent and child processes in the above code
if ($pid == -1) { // 创建错误,返回-1

    die(&#39;进程fork失败&#39;);

} else if (!$pid) { // $pid = 0

    // 子进程逻辑
    $time = microtime(true);
    echo "我是子进程:{$time}".NEWLINE;
} else if ($pid) { // $pid > 0, 如果fork成功,返回子进程id

    // 父进程逻辑
    $time = microtime(true);
    echo "我是父进程:{$time}".NEWLINE;

}
Copy after login

Execution results

当前进程:17472

我是父进程:1528697500.2961

我是子进程:1528697500.2961
Copy after login
Copy after login

Test summary

Fork will first execute the parent process logic and then execute the child process logic

The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP uses reflection to obtain classes and some basic applications

The above is the detailed content of PHP's pcntl process control pcntl_fork. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!