This article will explain the following contents:

PHP native process functions are introduced

PHP implements multiadic code

PHP native process functions

Although PHP is the best language in the world, multi-process, process management is still relatively new to phpers. This article will show you how to implement multiple processes using PHP native code.

First, we’ll look at PHP’s multi-process functions, such as pCNTL_fork, pCNTL_wait, pCNTL_waitpid, etc.

Pcntl_fork (void) : int

This function creates a child process and returns the process ID of type int. Both the parent and child processes proceed from fork.

When the child process is successfully created, 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. When the creation fails, -1 is returned in the parent context, the child process is not created, and a PHP error is raised.

Pcntl_wait (int & status[, intStatus [,intstatus [,intoptions = 0]) : int

This function waits for or returns the state of the child process at fork. This function suspends execution of the current process until a child process exits or a signal is sent to interrupt the current process or to call a signal handler. If a child process has exited by the time this function is called, it returns immediately.

This function returns the number of the exiting subprocess, -1 in case of an error, or 0 if WNOHANG is provided as an option and no subprocesses are available.

Pcntl_waitpid (int pid, int &status [, int $options = 0]) : int

This function waits for or returns the state of the child process at fork. Calling this function suspends execution of the current process until the process with the process number specified by the pid parameter exits, or a signal is received requesting that the current process be interrupted or a signal handler is called.

Pcntl_waitpid () returns the process number of the exiting child process, -1 in case of an error, or 0 if WNOHANG is provided as an option and no child process is available.

Pcntl_signal (int SIGNO, CallBackSIGno, callBackSIGno, callBackHandler [, bool $RESTART_sySCalls = true]) : bool

This function installs a signal handler for the signal specified by SIGNO.

Signo indicates the signal number.

Handler is a signal handler. It can be a user-created function or method, or it can be SIG_IGN (ignoring the signal handler) or SIG_DFL (the default signal handler).

Restart_syscalls specifies whether the system call restart is available when the signal arrives.

The function returns a bool, true on success or false on failure.

2. PHP implements multi-process code

[! [attachments-2020-08-iNu0GudH5f44bd130a508.jpg](https://six.club/image/show/attachments-2020-08-iNu0GudH5f44bd130a508.jp g)](https://six.club/image/show/attachments-2020-08-iNu0GudH5f44bd130a508.jpg)[! [attachments-2020-08-0L5U6GyT5f44bd192cc69.jpg](https://six.club/image/show/attachments-2020-08-0L5U6GyT5f44bd192cc69.jp g)](https://six.club/image/show/attachments-2020-08-0L5U6GyT5f44bd192cc69.jpg)

If you have any questions, you can leave a message for mutual discussion.