read()挂在僵尸进程上 [英] read() hangs on zombie process

查看:93
本文介绍了read()挂在僵尸进程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环,通过将子进程的stdout重定向到父进程,使用阻塞的I/O从子进程读取数据.通常,一旦子进程退出,在这种情况下将返回阻塞read(),因为从中读取的管道已被子进程关闭.

I have a while loop that reads data from a child process using blocking I/O by redirecting stdout of the child process to the parent process. Normally, as soon as the child process exits, a blocking read() in this case will return since the pipe that is read from is closed by the child process.

现在我有一个情况,对于完成的子进程,read()调用不会退出.子进程最终以僵尸状态结束,因为操作系统正在等待我的代码获得它,但是相反,我的代码在read()调用中被阻止.

Now I have a case where the read() call does not exit for a child process that finishes. The child process ends up in a zombie state, since the operating system is waiting for my code to reap it, but instead my code is blocking on the read() call.

在挂起时,子进程本身没有任何子进程正在运行,并且在/proc/<child process PID>/fd中查看时,我看不到列出的任何文件描述符.但是,子进程确实分叉了两个守护进程,它们的目的似乎是监视子进程(子进程是我无法控制的专有应用程序,因此很难确定).

The child process itself does not have any child processes running at the time of the hang, and I do not see any file descriptors listed when looking in /proc/<child process PID>/fd. The child process did however fork two daemon processes, whose purpose seems to be to monitor the child process (the child process is a proprietary application I do not have any control over, so it is hard to say for sure).

从终端运行时,我尝试从read()退出的子进程会自动退出,然后守护进程也将其分叉并终止.

When run from a terminal, the child process I try to read() from exits automatically, and in turn the daemon processes it forked terminate as well.

Linux版本是4.19.2.

Linux version is 4.19.2.

在这种情况下read()不返回的原因可能是什么?

What could be the reason of read() not returning in this case?

跟进:如何避免挂在以下情况下?

推荐答案

但是子进程确实派生了两个守护进程... read()在这种情况下不返回的原因可能是什么?

The child process did however fork two daemon processes ... What could be the reason of read() not returning in this case?

当子进程终止时,分叉的进程仍具有打开的文件描述符.因此read调用永远不会返回0.

Forked processes still have the file descriptor open when the child terminates. Hence read call never returns 0.

这些守护进程应关闭所有文件描述符并打开文件进行记录.

Those daemon processes should close all file descriptors and open files for logging.

这篇关于read()挂在僵尸进程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆