waitpid 子进程不成功

waitpid for child process not succeeding

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

编辑:errno 是 10,waitpid returns -1。这是在 linux 平台上。

没有代码很难调试,但是errno10是ECHILD

根据手册页,返回如下:

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

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

注意括号部分:

  • "This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN" - 如果您已将 SIGCHLD 的信号处理程序设置为 SIG_IGN,则wait 实际上是自动完成的,因此 waitpid 不会工作,因为子进程已经终止(不会进入僵尸状态)。

  • "See also the Linux Notes section about threads." - 在Linux中,线程本质上是进程。 Modern linux 将允许一个线程等待其他线程的子线程(前提是它们在同一个线程组中——广义上是父进程)。如果您使用的是 2.4 之前的 Linux,则情况并非如此。有关详细信息,请参阅 __WNOTHREAD 上的文档。

我猜线程的东西是转移注意力的东西,问题实际上是信号处理程序,因为这符合你的说法'the process is killed without becoming a zombie.'