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.