Example of PCNTL extension implementation of multi-process services in PHP CLI mode

Source: Internet
Author: User
Tags error handling php cli php error


PHP can implement process control through PCNTL extension, such as process creation, signal processing, process interrupt judgment, etc. However, you can operate only in CLI mode.
The signaling mechanism of PCNTL is based on the ticks mechanism. Therefore, you need to add the declare (ticks = n) syntax structure before using the signal correlation function.
Pcntl_alarm (int $seconds) sends a SIGALRM signal to the process after a specified number of seconds
pcntl_signal (int $signo, callback $handler [, BOOL $restart _syscalls]) sets the callback function for the specified signal $signo
DECLARE (ticks = 1);

function Signal_handler ($signal) {
Print "Caught sigalrm\n";
Pcntl_alarm (3);
}

Pcntl_signal (SIGALRM, "Signal_handler", true);
Pcntl_alarm (3);

for (;;) {
}

Pcntl_exec (string $path [, array $args [, array $envs]]) executes the specified command, ends after execution, and does not follow
$dir = '/root/';
$cmd = ' ls ';
$option = ' l ';
$pathtobin = '/bin/ls ';

$arg = Array ($cmd, $option, $dir);
Pcntl_exec ($pathtobin, $arg);

Echo ' won't be exec here ';

Pcntl_fork (void) Fork subprocess for the current process
At this point, the resulting fork return value is the subprocess number (>0) during the execution of the parent process, and when it fails, the parent process context returns-1, does not create a child process and throws a PHP error;
The blocking of the parent process blocks the child process at the same time. But the end of the parent process does not affect the operation of the child process;
The child process starts execution (including this function) from the statement that executes Pcntl_fork (), but at this point it returns 0 (representing this is a child process). It is preferable to have an exit statement in the code block of a child process that ends immediately after the child process is executed.
int pcntl_wait (int & $status [, int $options = 0]) waits for or returns fork status of a child process
After the child process call ends, it is not completely destroyed, but it becomes a zombie process, not a memory, only a list of processes. You need to call the parent process to wait for the child process to finish. If the parent process exits before the subprocess, the INIT process will manage the zombie process and it can be purged. The second argument sets the blocking mode:
1. wuntraced blocking mode calls, the function return value is the PID of the subprocess, if no child process return value is-1;
2. Wnohang non-blocking Invocation, the function can also return 0 when a child process with a child process is running but does not end.
$pid = Pcntl_fork ();
Both the parent process and the child process execute the following code
if ($pid = = 1) {
Error handling: Returns-1 when the child process is created failed.
Die (' could not fork ');
else if ($pid) {
The parent process will get the child process number, so this is the logic that the parent process executes
Pcntl_wait ($status, wuntraced); Wait for child process interrupts to prevent child processes from becoming zombie processes.
echo "OK". Php_eol;
else if ($pid = = 0) {
The child process gets a $pid of 0, so here is the logic that the subprocess executes.
echo "Child process runs." Getmypid (). Php_eol;
Sleep (5);
Exit
}

echo "Parent process runs." Getmypid (). Php_eol;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.