Home  >  Article  >  Backend Development  >  Detailed explanation of the solution to the zombie process problem in PHP multi-process

Detailed explanation of the solution to the zombie process problem in PHP multi-process

黄舟
黄舟Original
2017-10-16 09:11:451804browse

This article mainly introduces the relevant information for understanding the zombie process problem in PHP multi-process programming. I hope this article can help everyone and let everyone master this part of the content. Friends in need can refer to it

Understanding of the zombie process problem in PHP multi-process programming

Using the pcntl_fork function can allow PHP to achieve the effect of multi-process concurrent or asynchronous processing: http://www.jb51.net/article/ 125789.htm

Then the problem is that the process we generate needs to be controlled and cannot be ignored. The most basic way is to fork the process and kill the process.

By using the pcntl_fork function, we already have a new child process, and the child process will then complete what we need to process, so let's call it service() for the time being, and we need many service() For processing, refer again to our previous requirements. The parent process needs to read the configuration file in a loop and wait for the file to change. By using pcntl_fork, we can easily write the following code:


$res = config();
//kill进程
for($i = 0; $i < $res[sum]; $i++) {
  $pid = pcntl_fork();
  if ($pid == 0) {
    service();
    return;
  }
}

Where commented in the code, we need to kill the process when the configuration file changes. , the way to kill the process is very simple, you can use the kill command to kill it directly, for example (assuming the pid is 123):


1 kill 123

But we found that using this to kill The process method does not really kill the process. After the child process is killed, it still occupies the resources of this process. We become a zombie process. Zombie processes cannot be killed using the kill command. There are only two ways we can solve this problem.

1. shutdown

2. Kill the parent process of the process.

But neither of these methods will work, because the purpose of this program is to monitor and reside in the server, the server cannot be shut down, and the parent process cannot be killed. At this time we saw the official document’s explanation of the fork method:


pcntl_wait($status); //等待子进程中断,防止子进程成为僵尸进程。

It turns out that there is a way to prevent the process from becoming a zombie process, but the code given by the official website is like this of:


$pid = pcntl_fork();
//父进程和子进程都会执行下面代码
if ($pid == -1) {
  //错误处理:创建子进程失败时返回-1.
   die(&#39;could not fork&#39;);
} else if ($pid) {
   //父进程会得到子进程号,所以这里是父进程执行的逻辑
   pcntl_wait($status); //等待子进程中断,防止子进程成为僵尸进程。
} else {
   //子进程得到的$pid为0, 所以这里是子进程执行的逻辑。
}

What does it mean? That is, the parent process will wait for the child process to run. After the child process finishes running, it will proceed to the next step and the zombie process will also be eliminated. But this does not meet our needs. Our sub-process is an infinite loop program, constantly searching for output, and the update is not finished, and what we need is asynchronous processing rather than synchronization. But can this method be used? Actually of course you can.

This function is explained in the pcntl_wait documentation:

The wait function scrapes the execution of the current process until a child process exits or receives a signal requesting to interrupt the current process or call a signal processing function. If a child process has exited when calling this function (commonly known as a zombie process), this function returns immediately. All system resources used by the child process will be released. Please see your system's wait(2) manual for detailed specifications on how wait works on your system.

We found that when this function finds that the child process has become a zombie process, it will release the resources of the zombie process - provided that the zombie process is a child process of the parent process. Then we can cleverly use this method to let these zombie processes release resources, so we have the following code:


 posix_kill(123, 9);
 pcntl_wait($status);

In this way, we first use kill to kill this process, this The process will no longer run, but this process has become a zombie process, occupying resources. In the next sentence, we will execute pcntl_wait() to let these zombie processes release resources. In this way, the child process is truly terminated, and the zombie process is eliminated.

The above is the detailed content of Detailed explanation of the solution to the zombie process problem in PHP multi-process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn