Home > Backend Development > PHP7 > body text

PHP7 orphan process and zombie process

藏色散人
Release: 2023-02-17 12:02:01
forward
3266 people have browsed it

Basic concepts

We know that in unix/linux, under normal circumstances, the child process is created through the parent process, and the child process creates a new process. The end of the child process and the running of the parent process are an asynchronous process, that is, the parent process can never predict when the child process will end. When a process completes its work and terminates, its parent process needs to call the wait() or waitpid() system call to obtain the termination status of the child process.

Orphan process

If a parent process exits while one or more of its child processes are still running, those child processes will become orphan processes. The orphan process will be adopted by the init process (process number is 1), and the init process will complete the status collection work for them.

Zombie Process

A process uses fork to create a child process. If the child process exits and the parent process does not call wait or waitpid to obtain the status information of the child process, then The process descriptor of the child process is still saved in the system. This process is called a zombie process.

Problems and Hazards

Unix provides a mechanism to ensure that as long as the parent process wants to know the status information when the child process ends, it can get it. This mechanism is: when each process exits, the kernel releases all the resources of the process, including open files, occupied memory, etc. However, certain information is still retained for it (including the process ID, the termination status of the process, the amount of CPU time taken by the process, etc.). It is not released until the parent process retrieves it through wait/waitpid. But this leads to problems. If the process does not call wait/waitpid, the retained information will not be released, and its process number will always be occupied. However, the process number that the system can use is limited. If a large number of If a zombie process is generated, the system will not be able to generate new processes because there is no available process number. This is the harm of zombie processes and should be avoided.

An orphan process is a process without a parent process. The important task of the orphan process falls on the init process. The init process is like a civil affairs bureau, responsible for handling the aftermath of the orphan process. Whenever an orphan process appears, the kernel sets the parent process of the orphan process to init, and the init process will cyclically wait() its exited child processes. In this way, when an orphan process ends its life cycle miserably, the init process will handle all its aftermath on behalf of the party and the government. Therefore, orphan processes do no harm.

Any child process (except init) does not disappear immediately after exit(), but leaves a data structure called a zombie process (Zombie), waiting for the parent process to process. This is the stage that every child process goes through at the end. If the child process does not have time to process after exit(), then you can use the ps command to see that the status of the child process is "Z". If the parent process can handle it in time, it may be too late to see the zombie state of the child process using the ps command, but this does not mean that the child process will not go through the zombie state. If the parent process exits before the child process ends, the child process will be taken over by init. init will process the child process in zombie state as the parent process.

Zombie Process Hazard Scenario

For example, there is a process that regularly generates a child process. This child process needs to do very few things. After completing it, it should do what it should do. After the child process exits, it exits, so the life cycle of this child process is very short. However, the parent process only generates new child processes, and does not care about what happens after the child process exits. In this way, the system runs for a period of time. After that, there will be many zombie processes in the system. If you use the ps command to view it, you will see many processes with status Z. Strictly speaking, the zombie process is not the source of the problem. The culprit is the parent process that generates a large number of zombie processes. Therefore, when we are looking for how to eliminate a large number of zombie processes in the system, the answer is to shoot the culprit that generated a large number of zombie processes (that is, sending a SIGTERM or SIGKILL signal through kill). After the culprit process is shot, the zombie processes it generates become orphan processes. These orphan processes will be taken over by the init process. The init process will wait() these orphan processes and release the resources they occupy in the system process table. In this way, These orphan processes that have died can go away in peace.

Orphan process and zombie process test

1. The orphan process is adopted by the init process

$pid = pcntl_fork();
if ($pid > 0) {
    // 显示父进程的进程ID,这个函数可以是getmypid(),也可以用posix_getpid()
    echo "Father PID:" . getmypid() . PHP_EOL;
    // 让父进程停止两秒钟,在这两秒内,子进程的父进程ID还是这个父进程
    sleep(2);
} else if (0 == $pid) {
    // 让子进程循环10次,每次睡眠1s,然后每秒钟获取一次子进程的父进程进程ID
    for ($i = 1; $i <= 10; $i++) {
        sleep(1);
        // posix_getppid()函数的作用就是获取当前进程的父进程进程ID
        echo posix_getppid() . PHP_EOL;
    }
} else {
    echo "fork error." . PHP_EOL;
}
Copy after login

Test result:

php daemo001.php
Father PID:18046
18046
18046
www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ 1
1
1
1
1
1
1
1
Copy after login

2. Zombie processes and hazards

Execute the following code php zombie1.php 

$pid = pcntl_fork();
if( $pid > 0 ){
    // 下面这个函数可以更改php进程的名称
    cli_set_process_title(&#39;php father process&#39;);
    // 让主进程休息60秒钟
    sleep(60);
} else if( 0 == $pid ) {
    cli_set_process_title(&#39;php child process&#39;);
    // 让子进程休息10秒钟,但是进程结束后,父进程不对子进程做任何处理工作,这样这个子进程就会变成僵尸进程
    sleep(10);
} else {
    exit(&#39;fork error.&#39;.PHP_EOL);
}
Copy after login

Execution results, another terminal window

www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18458  0.5  1.2 204068 25920 pts/1    S+   16:34   0:00 php father process
www      18459  0.0  0.3 204068  6656 pts/1    S+   16:34   0:00 php child process
www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18458  0.0  1.2 204068 25920 pts/1    S+   16:34   0:00 php father process
www      18459  0.0  0.0      0     0 pts/1    Z+   16:34   0:00 [php] <defunct>
Copy after login

By executing the ps -aux command, you can see that when the program is running within the first ten seconds, the status of the php child process is listed as [S]. However, after ten seconds, this status becomes [Z], also It becomes a zombie process that harms the system.

So, here’s the question? How to avoid zombie processes?

PHP通过 pcntl_wait() pcntl_waitpid() 两个函数来帮我们解决这个问题。了解Linux系统编程的应该知道,看名字就知道这其实就是PHP把C语言中的 wait() waitpid() 包装了一下。

通过代码演示 pcntl_wait() 来避免僵尸进程。

pcntl_wait() 函数:

这个函数的作用就是 “ 等待或者返回子进程的状态 ”,当父进程执行了该函数后,就会阻塞挂起等待子进程的状态一直等到子进程已经由于某种原因退出或者终止。

换句话说就是如果子进程还没结束,那么父进程就会一直等等等,如果子进程已经结束,那么父进程就会立刻得到子进程状态。这个函数返回退出的子进程的进程 ID 或者失败返回 -1。

执行以下代码 zombie2.php

$pid = pcntl_fork();
if ($pid > 0) {
    // 下面这个函数可以更改php进程的名称
    cli_set_process_title(&#39;php father process&#39;);
    // 返回$wait_result,就是子进程的进程号,如果子进程已经是僵尸进程则为0
    // 子进程状态则保存在了$status参数中,可以通过pcntl_wexitstatus()等一系列函数来查看$status的状态信息是什么
    $wait_result = pcntl_wait($status);
    print_r($wait_result);
    print_r($status);
    // 让主进程休息60秒钟
    sleep(60);
} else if (0 == $pid) {
    cli_set_process_title(&#39;php child process&#39;);
    // 让子进程休息10秒钟,但是进程结束后,父进程不对子进程做任何处理工作,这样这个子进程就会变成僵尸进程
    sleep(10);
} else {
    exit(&#39;fork error.&#39; . PHP_EOL);
}
Copy after login

在另外一个终端中通过ps -aux查看,可以看到在前十秒内,php child process 是 [S+] 状态,然后十秒钟过后进程消失了,也就是被父进程回收了,没有变成僵尸进程。

www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18519  0.5  1.2 204068 25576 pts/1    S+   16:42   0:00 php father process
www      18520  0.0  0.3 204068  6652 pts/1    S+   16:42   0:00 php child process
www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18519  0.0  1.2 204068 25576 pts/1    S+   16:42   0:00 php father process
Copy after login

但是,pcntl_wait() 有个很大的问题,就是阻塞。父进程只能挂起等待子进程结束或终止,在此期间父进程什么都不能做,这并不符合多快好省原则,所以 pcntl_waitpid() 闪亮登场。pcntl_waitpid( pid, &status, $option = 0 )的第三个参数如果设置为WNOHANG,那么父进程不会阻塞一直等待到有子进程退出或终止,否则将会和pcntl_wait()的表现类似。

修改第三个案例的代码,但是,我们并不添加WNOHANG,演示说明pcntl_waitpid()功能:

$pid = pcntl_fork();
if ($pid > 0) {
    // 下面这个函数可以更改php进程的名称
    cli_set_process_title(&#39;php father process&#39;);
    // 返回值保存在$wait_result中
    // $pid参数表示 子进程的进程ID
    // 子进程状态则保存在了参数$status中
    // 将第三个option参数设置为常量WNOHANG,则可以避免主进程阻塞挂起,此处父进程将立即返回继续往下执行剩下的代码
    $wait_result = pcntl_waitpid($pid, $status);
    var_dump($wait_result);
    var_dump($status);
    // 让主进程休息60秒钟
    sleep(60);
} else if (0 == $pid) {
    cli_set_process_title(&#39;php child process&#39;);
    // 让子进程休息10秒钟,但是进程结束后,父进程不对子进程做任何处理工作,这样这个子进程就会变成僵尸进程
    sleep(10);
} else {
    exit(&#39;fork error.&#39; . PHP_EOL);
}
Copy after login

下面是运行结果,一个执行php zombie3.php 程序的终端窗口

www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ php zombie3.php
int(18586)
int(0)
^C  
Copy after login

ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程。常用于终止正在运行的程序。

下面是ps -aux终端窗口

www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18605  0.3  1.2 204068 25756 pts/1    S+   16:52   0:00 php father process
www      18606  0.0  0.3 204068  6636 pts/1    S+   16:52   0:00 php child process
www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18605  0.1  1.2 204068 25756 pts/1    S+   16:52   0:00 php father process
www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18605  0.0  1.2 204068 25756 pts/1    S+   16:52   0:00 php father process
www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php  // ctrl-c 后不再被阻塞
www@iZ2zec3dge6rwz2uw4tveuZ:~$
Copy after login

实际上可以看到主进程是被阻塞的,一直到第十秒子进程退出了,父进程不再阻塞  

修改第四段代码,添加第三个参数WNOHANG,代码如下:

$pid = pcntl_fork();
if ($pid > 0) {
    // 下面这个函数可以更改php进程的名称
    cli_set_process_title(&#39;php father process&#39;);
    // 返回值保存在$wait_result中
    // $pid参数表示 子进程的进程ID
    // 子进程状态则保存在了参数$status中
    // 将第三个option参数设置为常量WNOHANG,则可以避免主进程阻塞挂起,此处父进程将立即返回继续往下执行剩下的代码
    $wait_result = pcntl_waitpid($pid, $status, WNOHANG);
    var_dump($wait_result);
    var_dump($status);
    echo "不阻塞,运行到这里" . PHP_EOL;
    // 让主进程休息60秒钟
    sleep(60);
} else if (0 == $pid) {
    cli_set_process_title(&#39;php child process&#39;);
    // 让子进程休息10秒钟,但是进程结束后,父进程不对子进程做任何处理工作,这样这个子进程就会变成僵尸进程
    sleep(10);
} else {
    exit(&#39;fork error.&#39; . PHP_EOL);
}
Copy after login

执行 php zombie4.php

www@iZ2zec3dge6rwz2uw4tveuZ:~/test$ php zombie4.php
int(0)
int(0)
不阻塞,运行到这里  
Copy after login

另一个ps -aux终端窗口

www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18672  0.3  1.2 204068 26284 pts/1    S+   17:00   0:00 php father process
www      18673  0.0  0.3 204068  6656 pts/1    S+   17:00   0:00 php child process
www@iZ2zec3dge6rwz2uw4tveuZ:~$ ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
www      18672  0.0  1.2 204068 26284 pts/1    S+   17:00   0:00 php father process
www      18673  0.0  0.0      0     0 pts/1    Z+   17:00   0:00 [php] <defunct>
Copy after login

实际上可以看到主进程是被阻塞的,一直到第十秒子进程退出了,父进程不再阻塞。  

问题出现了,竟然php child process进程状态竟然变成了[Z+],这是怎么搞得?回头分析一下代码:
我们看到子进程是睡眠了十秒钟,而父进程在执行pcntl_waitpid()之前没有任何睡眠且本身不再阻塞,所以,主进程自己先执行下去了,而子进程在足足十秒钟后才结束,进程状态自然无法得到回收。

如果我们将代码修改一下,就是在主进程的pcntl_waitpid()前睡眠15秒钟,这样就可以回收子进程了。但是即便这样修改,细心想的话还是会有个问题,那就是在子进程结束后,在父进程执行pcntl_waitpid()回收前,有五秒钟的时间差,在这个时间差内,php child process也将会是僵尸进程。那么,pcntl_waitpid()如何正确使用啊?这样用,看起来毕竟不太科学。
那么,是时候引入信号学了!

The above is the detailed content of PHP7 orphan process and zombie process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template