子进程的waitpid不成功 [英] waitpid for child process not succeeding
问题描述
我正在使用execv
开始一个进程并将其写入文件.我同时启动一个线程来监视文件,以便使用stat.st_size
使其大小不超过特定限制.现在,当达到限制时,子进程I waitpid
,但这会引发错误,而我在后台启动的进程将成为僵尸.当我从主线程使用相同的waitpid
停止时,该进程将被杀死而不会成为僵尸.有什么想法吗?
errno
为10,waitpid
返回-1.这是在linux平台上.
没有代码很难调试,但是errno
10是ECHILD
.
根据手册页,返回如下:
ECHILD
(对于waitpid()
或waitid()
)pid
(waitpid()
)或idtype
和id
(waitid()
)指定的进程不存在或不是以下子项调用过程. (如果将SIGCHLD
的操作设置为SIG_IGN
,这可能会发生在自己的孩子身上.另请参见关于线程的Linux Notes部分.)
简而言之,您指定的pid
不是调用waitpid()
的进程的子级(或者不再是,也许是因为它已终止).
请注意括号部分:
-
如果将
SIGCHLD
的动作设置为SIG_IGN
,则可能发生在自己的孩子身上" -如果已为SIGCHLD
设置了信号处理程序是SIG_IGN
,wait
会自动有效地执行,因此waitpid
将不起作用,因为孩子将已经终止(不会进入僵尸状态). -
另请参阅有关线程的Linux Notes部分." -在Linux中,线程本质上是进程.现代linux将允许一个线程等待其他线程的子代(前提是它们在同一线程组中-广泛来说是父进程).如果您使用的是2.4之前的Linux,则不是这种情况.有关详细信息,请参见
__WNOTHREAD
上的文档.
我猜线程是红色鲱鱼,实际上是信号处理程序,因为这符合您的声明进程被杀死而不会成为僵尸." >
I am starting a process using execv
and letting it write to a file. I start a thread simultaneously that monitors the file so that it's size does not exceed a certain limit using stat.st_size
. Now, when the limit is hit, I waitpid
for the child process, but this throws an error and the process I start in the background becomes a zombie. When I do the stop using the same waitpid
from the main thread, the process is killed without becoming a zombie. Any ideas?
Edit: The errno
is 10 and waitpid
returns -1. This is on a linux platform.
This is difficult to debug without code, but errno
10 is ECHILD
.
Per the man page, this is returned as follows:
ECHILD
(forwaitpid()
orwaitid()
) The process specified bypid
(waitpid()
) oridtype
andid
(waitid()
) does not exist or is not a child of the calling process. (This can happen for one's own child if the action forSIGCHLD
is set toSIG_IGN
. See also the Linux Notes section about threads.)
In short, the pid
you are specifying is not a child of the process calling waitpid()
(or is no longer, perhaps because it has terminated).
Note the parenthetical section:
"This can happen for one's own child if the action for
SIGCHLD
is set toSIG_IGN
" - if you've set up a signal handler forSIGCHLD
to beSIG_IGN
, thewait
is effectively done automatically, and thereforewaitpid
won't work as the child will have already terminated (will not go through zombie state)."See also the Linux Notes section about threads." - In Linux, threads are essentially processes. Modern linux will allow one thread to wait for children of other threads (provided they are in the same thread group - broadly parent process). If you are using Linux prior to 2.4, this is not the case. See the documentation on
__WNOTHREAD
for details.
I'm guessing the thread thing is a red herring, and the problem is actually the signal handler, as this accords with your statement 'the process is killed without becoming a zombie.'
这篇关于子进程的waitpid不成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!