子进程的waitpid不成功 [英] waitpid for child process not succeeding

查看:151
本文介绍了子进程的waitpid不成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用execv开始一个进程并将其写入文件.我同时启动一个线程来监视文件,以便使用stat.st_size使其大小不超过特定限制.现在,当达到限制时,子进程I waitpid,但这会引发错误,而我在后台启动的进程将成为僵尸.当我从主线程使用相同的waitpid停止时,该进程将被杀死而不会成为僵尸.有什么想法吗?

errno为10,waitpid返回-1.这是在linux平台上.

解决方案

没有代码很难调试,但是errno 10是ECHILD.

根据手册页,返回如下:

ECHILD(对于waitpid()waitid())pid(waitpid())或idtypeid(waitid())指定的进程不存在或不是以下子项调用过程. (如果将SIGCHLD的操作设置为SIG_IGN,这可能会发生在自己的孩子身上.另请参见关于线程的Linux Notes部分.)

简而言之,您指定的pid不是调用waitpid()的进程的子级(或者不再是,也许是因为它已终止).

请注意括号部分:

  • 如果将SIGCHLD的动作设置为SIG_IGN,则可能发生在自己的孩子身上" -如果已为SIGCHLD设置了信号处理程序是SIG_IGNwait会自动有效地执行,因此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 (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. (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.)

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 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).

  • "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屋!

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