简体   繁体   English

子进程的waitpid不成功

[英]waitpid for child process not succeeding

I am starting a process using execv and letting it write to a file. 我正在使用execv启动一个进程,并使其写入文件。 I start a thread simultaneously that monitors the file so that it's size does not exceed a certain limit using stat.st_size . 我同时启动一个线程来监视文件,以便使用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. 现在,当达到限制,我waitpid子进程,但是这将引发一个错误,我在后台启动过程将成为僵尸。 When I do the stop using the same waitpid from the main thread, the process is killed without becoming a zombie. 当我从主线程使用相同的waitpid停止时,该进程将被杀死而不会成为僵尸。 Any ideas? 有任何想法吗?

Edit: The errno is 10 and waitpid returns -1. 编辑: errno为10, waitpid返回-1。 This is on a linux platform. 这是在linux平台上。

This is difficult to debug without code, but errno 10 is ECHILD . 没有代码很难调试,但是errno 10是ECHILD

Per the man page, this is returned as follows: 对于手册页,返回如下:

ECHILD (for waitpid() or waitid() ) The process specified by pid ( waitpid() ) or idtype and id ( waitid() ) does not exist or is not a child of the calling process. ECHILD (用于waitpid()waitid() )由pidwaitpid() )或idtypeidwaitid() )指定的进程不存在,或者不是调用进程的子进程。 (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN . See also the Linux Notes section about threads.) (如果将SIGCHLD的操作设置为SIG_IGN这可能会发生在自己的孩子身上。另请参阅关于线程的Linux Notes部分。)

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). 简而言之,您指定的pid不是调用waitpid()的进程的子进程(或者不再是,可能是因为它已终止)。

Note the parenthetical section: 请注意括号部分:

  • "This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN " - if you've set up a signal handler for SIGCHLD to be SIG_IGN , the wait is effectively done automatically, and therefore waitpid won't work as the child will have already terminated (will not go through zombie state). “如果将SIGCHLD的操作设置为SIG_IGN这可能会发生在自己的孩子身上 -如果您已将SIGCHLD的信号处理程序设置为SIG_IGN ,则wait会自动有效地完成,因此waitpid不会像孩子将已经终止(不会进入僵尸状态)。

  • "See also the Linux Notes section about threads." “另请参阅有关线程的Linux Notes部分。” - In Linux, threads are essentially processes. -在Linux中,线程本质上是进程。 Modern linux will allow one thread to wait for children of other threads (provided they are in the same thread group - broadly parent process). 现代linux将允许一个线程等待其他线程的子代(前提是它们在同一线程组中-广泛来说是父进程)。 If you are using Linux prior to 2.4, this is not the case. 如果使用2.4之前的Linux,则不是这种情况。 See the documentation on __WNOTHREAD for details. 有关详细信息,请参见__WNOTHREAD上的文档。

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.' 我猜线程问题是一个红色的鲱鱼,实际上是信号处理程序,因为这符合您的说法: “进程被杀死而没有成为僵尸。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM