Home > headlines > Weibo Qzone WeChat pcntl_fork implements PHP multi-process

Weibo Qzone WeChat pcntl_fork implements PHP multi-process

小云云
Release: 2017-11-03 13:57:33
Original
1942 people have browsed it

What PHP engineers usually use the most is PHP-FMP for scheduling PHP processes. The use of PHP is not limited to Web, it can also be used for programming system tools, monitoring or operation and maintenance. Next, let’s talk about how to implement PHP multi-process programming.

First start a main process, which is used to read configuration information

For example, I read that I need to monitor 5 indicators. Next, the main process starts 5 sub-processes to monitor these 5 indicators respectively.

Simplified operation: one main process creates 5 sub-processes

To create a process, you need to use a function pcntl_fork() in PHP. Some people may not be familiar with this function, but they have been exposed to Linux. Anyone who has become C knows that there is a function called fork() in Linux, which is used to create child processes.

It should be noted that this function can only be used under Linux, and the pcntl extension needs to be installed.

The official document says this:

The pcntl_fork() function creates a child process. This child process is only different from its parent process in PID (process number) and PPID (parent process number). For details on how fork works on your system, consult your system's fork(2) manual.

When successful, the PID of the generated child process is returned in the parent process execution thread, and 0 is returned in the child process execution thread. On failure, -1 is returned in the parent process context, the child process is not created, and a PHP error is raised.

In this way, a child process can be created. After the child process is successfully created, the method after pcntl_fork() will be executed.


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