Examples of PHP multi-process pcntl_fork and pcntl_fork

Source: Internet
Author: User
Tags php error

Examples of PHP multi-process pcntl_fork and pcntl_fork

Example of pcntl_fork for PHP multi-process editing

In fact, PHP supports concurrency, but it is rarely used at ordinary times. Usually the most used should be to use the PHP-FMP to schedule the php process.

However, the use of PHP is not limited to Web. We can also use PHP to Program System Tools, perform monitoring or O & M. When using these directions, we can use more PHP features, such as concurrency (multi-process) and socket programming.

Next, let's talk about the PHP multi-process programming I met. The use of this multi-process has a background. The background is briefly described below.

I need a monitoring system. Of course, the PHP language is used. The monitoring system needs to monitor many system metrics. In order to enable every monitoring indicator to focus on its own tasks, you need to use a single process to monitor a metric, and another process to read the configuration. After obtaining the configuration, start each process according to the configuration.

So, we need to talk about multi-process.

  1. Start a main process to read configuration information. For example, I read 5 metrics that I need to monitor.
  2. Next, the main process starts five sub-processes to monitor these five indicators.
  3. After five metric monitoring processes are created, the main process performs monitoring configuration.
  4. Once the configuration changes, kill the previous process and recreate the process.

Relatively clear logic. Next we will simplify the operation: Simply put, a master process creates five sub-processes.

First, when creating a process, you need to use a function pcntl_fork () in php. Some of you may not be familiar with this function, but anyone who has been familiar with Linux C may know that there is a fork () in Linux () to create sub-processes. This function is equivalent to this function in Linux. Note that this function can be used in Linux, and the pcntl extension must be installed.

For how to use this function, we can refer to the official documentation: http://php.net/manual/zh/function.pcntl-fork.php

The official document says this:

The pcntl_fork () function creates a sub-process. This sub-process is only PID (process number) and PPID (parent process number) different from its parent process. For more information about how fork works in your system, see the fork (2) Manual of your system.

When the process succeeds, the PID of the child process is returned in the execution thread of the parent process, and 0 is returned in the execution thread of the child process. If a failure occurs,-1 is returned in the context of the parent process. Child processes are not created and a PHP error is thrown.

In this way, you can create a sub-process. After the sub-process is created successfully, the method after pcntl_fork () is executed. So how can we understand the return value of this function?

This is the case. When we call a function to create a process, the function execution takes some time, and the new process is created between the start and end of the function execution. In this way, the new process also executes this function, so the function also needs to return values. After a function is executed, both the parent process and child process are returned by the function. Because the parent process creates a child process, the child process does not create a new process, therefore, the sub-process does not return the result of this function, so it is assigned a 0 value. The parent process creates a child process, and the child process has a pid, so the pid of the process is obtained.

We can write a program to learn about it:

$pid = pcntl_fork();var_dump($pid);

This call outputs two values, but if we directly print only one value, that is, the pid of the sub-process, we can see two values using var_dump, is the pid of 0 and sub-process. The value 0 is returned by the sub-process.

After learning how to create a process, you can start creating the process. If we need to create five processes, I will create the process five times in a loop. The following code is obtained:

 $i=0; while($i!=5){  $pid = pcntl_fork();  echo $pid."---------hahah".$i++.PHP_EOL; }

In this case, write it, and run it. Ah? We found that there were not five processes. We found that there were many processes, and the last hahah4 output was 32. Why is it 32? Let's calculate it. 2 ^ 5 = 32. Why is the number of threads increasing exponentially?

It is not difficult to find out this, because each of our subsequent processes executes the while loop, and finally the exponential growth of the process-that is to say, the while loop is also carried in the fork. But we only need five processes. What should we do?

From the previous research on functions, we can see that the child process returns a value of 0, so we can know that 0 is the flag of the child process. We Can End Process execution by marking Child processes. So we can change our code to the following form:

$ I = 0; while ($ I! = 5) {$ pid = pcntl_fork (); echo $ pid. "--------- hahah ". $ I ++. PHP_EOL; if ($ pid = 0) {echo "sub-process ". PHP_EOL; return ;}}

Because 0 is actually a tag of the child process, the pid variable is actually 0 in the child process, so when the pid value is found to be 0, we can conclude that our current process is a sub-process and there is no need to let him execute while and create a sub-process, so after executing our content, return or exit to exit the execution. This ensures that we have created five processes instead of 32.

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!

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.