PHP multi-process programming zombie process problem

Source: Internet
Author: User
Tags signal handler

The previous article said that using the Pcntl_fork function allows PHP to implement multi-process concurrency or asynchronous processing. The problem is that the process we produce needs to be controlled, not ignored. The most basic way is to fork the process and kill the process.

By taking advantage of the pcntl_fork function, we have a new subprocess, and the sub-process next completes what we need to deal with, so let's call IT Service (), and we need a lot of service () to handle it again, referencing our previous requirements, The parent process needs to iterate through the configuration file and wait for the file to change. By way of Pcntl_fork, it is easy for us to write the following code:

$res = config (); // Kill Process  for ($i$i$res$i+ +)    {$pid = pcntl_fork ()    ; if ($pid = = 0) {        service ()        ; return ;    }}

Where the code is commented, we need to kill the process when it changes in the configuration file, and the way to kill the process is simple and can be killed directly using the KILL command, for example (assuming PID 123):

1 Kill 123

But we found that the way to kill the process did not really kill the process, the child process was killed after the death also occupied the resources of the process, we become a zombie process, the zombie process is not killed using the KILL command. To solve this problem, there are only two ways we can do it.

1. Shutdown

2. Kill the parent process of the process.

Neither of these methods is possible, because the purpose of the program is to monitor the resident server, the server cannot shut down, and the parent process cannot be killed. This is where we see the official document explaining the fork method:

1 pcntl_wait ($status// Wait for the child process to break to prevent the child process from becoming a zombie process. 

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

1 $pid=pcntl_fork ();2 //both the parent and child processes execute the following code3 if($pid= =-1) {4     //error Handling: Returns -1 when creating a child process failure.5       die(' could not fork ');6}Else if($pid) {7      //The parent process gets the child process number, so this is the logic that the parent process executes8Pcntl_wait ($status);//wait for the child process to break to prevent the child process from becoming a zombie process. 9}Else {Ten      //the child process gets a $pid of 0, so here is the logic that the child process executes.  One}

What do you mean? That is, the parent process waits for the child process to run, and when the child process finishes running, the next step is done, and the zombie process is eliminated. But here again and our needs do not meet, our child process for a dead loop of the program, constantly looking for output, but not the end of the time, and we need asynchronous processing instead of synchronization. But can this method be used? Actually, of course I can.

This function is explained in the pcntl_wait documentation:

The wait function shaves the execution of the current process until a child process exits or receives a signal that requires an interrupt to the current process or calls a signal handler function. If a child process has exited (commonly known as a zombie process) when calling this function, this function returns immediately. All system resources used by the child process will be freed. See the Wait (2) manual for your system for a detailed specification of what wait is working on your system.

We found that when this function discovers that a subprocess becomes a zombie process, it releases the resources of the zombie process--provided that the zombie process is a child of the parent process. Then we can skillfully use this method to let these zombie processes release resources, so we have the following code:

1 Posix_kill (123, 9); 2 pcntl_wait ($status);

So we first use kill to kill the process, the process will not run again, but this process becomes a zombie process, occupy the resources, we will execute the next sentence pcntl_wait () Let these zombie processes release resources, so that the child process is really terminated, the zombie process is eliminated.

PHP multi-process programming zombie process problem

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.