Understanding of zombie process in PHP multi process programming

  • 2021-08-10 06:57:53
  • OfStack

Understanding of zombie process in PHP multi-process programming

Using the pcntl_fork function, PHP can achieve the effect of multi-process concurrent or asynchronous processing: https://www.ofstack.com/article/125789. htm

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

By using the pcntl_fork function, we have a new child process, and the child process completes what we need to deal with next, so let's call it service () for the time being, and we need many service () to deal with it. Referring to our previous requirements again, the parent process needs 1 straight loop to read the configuration file and wait for the file to change. By using pcntl_fork, we can easily write the following code:


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

In the comments in the code, we need to kill the process when there is a change in the configuration file. The way to kill the process is very simple. You can use the kill command to kill it directly, such as (assuming pid is 123):


1 kill 123

However, we found that using this way to kill the process did not really kill the process. After this sub-process was killed, it also occupied the resources of this process. We became a zombie process, and the zombie process could not be killed by using kill command. There are only two ways we can solve this problem.

1. shutdown

2. Kill the parent of the process.

But neither method works, because the purpose of this program is to monitor resident in the server, the server cannot be shut down, and the parent process cannot be killed. At this time, we saw the explanation of fork method in the official document:


pcntl_wait($status); // Wait for the child process to interrupt to prevent the child process from becoming a zombie process. 

Originally, there is a way to prevent the process from becoming a zombie process, but the code given by official website is like this:


$pid = pcntl_fork();
// Both parent and child processes execute the following code 
if ($pid == -1) {
  // Error handling: Returns when child process creation fails -1.
   die('could not fork');
} else if ($pid) {
   // The parent process will get the child process number, so here is the logic executed by the parent process 
   pcntl_wait($status); // Wait for the child process to interrupt to prevent the child process from becoming a zombie process. 
} else {
   // Obtained by child process $pid For 0,  So here is the logic of child process execution. 
}

What do you mean? That is, the parent process will wait for the child process to run, and after the child process runs, it will proceed to the next step, and it will also eliminate the zombie process. But here and we do not meet the needs, our sub-process for a dead loop program, constantly looking for output, more than the end of the time, and we need asynchronous processing rather than synchronization. But can this method be used? Actually, of course.

This function is explained in the documentation for pcntl_wait as follows:

The wait function scrapes the execution of the current process until a child process exits or receives a signal to interrupt the current process or call a signal handler. 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 child processes will be released. Please refer to 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 skillfully 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, and this process will not run again, but this process has become a zombie process, occupying resources. We will execute pcntl_wait () once in the next sentence to let these zombie processes release resources, so that the sub-process is really terminated and the zombie process is eliminated.

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: