PHP multi-process programming: understanding of zombie processes; php programming of zombie Processes

Source: Internet
Author: User

PHP multi-process programming: understanding of zombie processes; php programming of zombie Processes

PHP multi-process programming: an understanding of zombie Processes

Using the pcntl_fork function can let PHP achieve multi-process concurrency or asynchronous processing effect: http://www.bkjia.com/article/125789.htm

The problem is that the processes we generate need to be controlled and cannot be ignored. The most basic method is fork process and killing process.

By using the pcntl_fork function, we have a new sub-process, and the sub-process completes the content we need to process next, so we will call it service () for the moment, in addition, we need to process a lot of services () and refer to our previous requirements again. The parent process needs to read the configuration file cyclically until the file changes. Through the pcntl_fork method, 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 the configuration file changes. The method to kill the process is very simple. You can use the kill command to directly kill the process, for example (if the pid is 123 ):

1 kill 123

However, we found that the method of killing a process does not actually kill the process. After the sub-process is killed, it still occupies the resources of the process and we become a zombie process, botnets cannot be killed by using the kill command. There are only two ways to solve this problem.

1. shutdown

2. Killed the parent process of the process.

However, neither of the two methods works, because the program aims to monitor the resident in the server, the server cannot be shut down, and the parent process cannot be killed. At this time, we can see the official documentation's explanation of the fork method:

Pcntl_wait ($ status); // wait for the child process to be interrupted to prevent the child process from becoming a zombie process.

There was a way to prevent the process from becoming a zombie process, but the Code provided on the official website is like this:

$ Pid = pcntl_fork (); // both the parent and child processes will execute the following code if ($ pid =-1) {// error handling: -1 is returned if the sub-process fails to be created. die ('could not fork');} else if ($ pid) {// The parent process will get the sub-process number, so here is the logic pcntl_wait ($ status) executed by the parent process; // wait until the child process is interrupted to prevent the child process from becoming a zombie process .} Else {// The $ pid obtained by the sub-process is 0, so here is the logic of sub-process execution .}

What does it mean? That is, the parent process will wait for the sub-process to run. After the sub-process is completed, the next step will be performed and the zombie process will be eliminated. However, this is not in line with our requirements. Our sub-process is an endless loop program, constantly searching for output, and even if it is not over, what we need is asynchronous processing rather than synchronization. But can this method be used? Of course.

The pcntl_wait document explains this function as follows:

The wait function is used to execute the current process until a sub-process exits or receives a signal to interrupt the current process or call a signal processing function. If a sub-process has exited (also known as a zombie process) when calling this function, this function returns immediately. All system resources used by the sub-process will be released. For more information about how wait works on your system, see the wait (2) Manual of your system.

We found that when this function finds that a child process becomes a zombie process, it will release the resources of the zombie process, provided that the zombie process is the child process of the parent process. Then we can 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 will first use kill to kill this process and it will not run any more. However, this process becomes a zombie process and occupies resources. In the next sentence, we will execute pcntl_wait () let these zombie processes release resources so that the sub-processes are actually terminated and the zombie process is eliminated.

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.