PHP multi-process Programming Pcntl_fork solution

Source: Internet
Author: User
Tags php language php error

In fact, PHP is supported by concurrency, but usually rarely used. The most commonly used should be the use of PHP-FMP scheduling PHP process it.

However, the use of PHP is not limited to the web, we can also use PHP to do the System tool class programming, monitoring or operations. When using these directions, we can use more features of PHP, such as concurrency (multi-process), socket programming, and so on.

So let's talk about the multi-process programming that I've encountered in PHP. The use of this multi-process is a background, the following vague description of the background.

I need a monitoring system, of course, using the PHP language, monitoring system needs to monitor many kinds of system indicators, in order to allow each monitoring indicators to concentrate on doing their own things, you need to use a single process to monitor an indicator, there is a process to read the configuration, after the configuration, according to the configuration to start each process.

Well, that requires a lot of the process I'm talking about.

    1. Start a master process, which is used by the main process to read configuration information. For example, I read the 5 metrics I need to monitor
    2. The main process then starts 5 sub-processes and monitors the 5 metrics separately.
    3. After creating 5 metrics to monitor the process, the master process listens to the configuration.
    4. Once the configuration has changed, kill the previous process and recreate the process.

Relatively clear logic. So then we'll simplify the operation: Simply put, a master process creates 5 sub-processes.

First of all, the creation process in PHP need to use a function pcntl_fork (), this function may be some of the classmates are not familiar with, but the people who have been in contact with Linux C have known that Linux has a function called fork (), used to create a child process. This function and Linux under this function is a meaning. It is important to note that this function is available under Linux and requires the installation of PCNTL extensions.

For how this function is used, we can check the official documentation: http://php.net/manual/zh/function.pcntl-fork.php

This is what the official document says:

The Pcntl_fork () function creates a child process that is different from its parent process, only the PID (process number) and Ppid (the parent process number). For more information on how to work with your system, please refer to the fork (2) Manual of your system.

On success, the PID of the resulting child process is returned within the parent process execution thread, returning 0 within the child process execution thread. On failure, the parent process context returns-1, the child process is not created, and a PHP error is raised.

This allows you to create a child process that will execute the Pcntl_fork () after the child process is created successfully. So how do we understand the return value of this function?

So, when we call the function to create the process, the function executes with time, and the new process is created just between the start and end of the function execution, so that the new process also executes the function, so the function also needs to have a return value. Then for the function to execute once, both the parent and child processes are subject to the return value of the function, because the parent process creates a child process, and the child process does not create a new process, so the child process has no effect on the return of the function, so he is assigned a 0. And the parent process created the child process, the child process exists PID, so we get the PID of that process.

We can write a procedure to understand:

1 $pid = pcntl_fork (); 2 Var_dump ($pid);

This call will output two values, but if we can see only one value in print, that is, the PID of the subprocess, but using var_dump we can see two values, which is 0 and the PID of the child process. 0 This value is the child process returned.

So how to create a process, then you can start creating the process, we need to create 5 processes, then I loop 5 times to create the process. Get the following code:

1 $i=0; 2  while ($i!=5) {3     $pid = pcntl_fork (); 4     Echo $pid. " ---------Hahah ". $i+ +. Php_eol ; 5 }

So write it, then run it. Huh? Found not 5 processes Ah, found that there are many processes, and the last Hahah4 this output has 32, why is 32? Let's count. 2^5=32, why did the last number of threads grow exponentially?

It is not difficult to find this, because each of us then executes the while loop, which eventually becomes the exponential growth of the process-that is, when the fork is brought in with the while loop. But we're just going to need 5 of processes. What do we do?

Through the previous study of the function, we can see that the child process will return a value of 0, then we can know that 0 is a child process of the tag. We can end process execution with a child process token. So we can change our code to the following form:

1 $i=0;2  while($i!=5){3     $pid=pcntl_fork ();4     Echo $pid." ---------Hahah ".$i++.Php_eol;5     if($pid= = 0) {6         Echo"Child process".Php_eol;7         return;8     }9}

Since 0 is actually a token of the child process, then the PID variable is actually 0 in the subprocess, so when we find that the PID value is 0, we can conclude that our current process is a child process, that we do not need to let him execute while and create the child process of the subprocess. So it's good to return or exit the execution after we have finished our content. This will ensure that we do create 5 processes instead of 32.

PHP multi-process Programming Pcntl_fork solution

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.