本文介绍了为什么waitid直到孩子终止都不会阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void *stack;
stack = malloc(STACK_SIZE);
if (-1 == clone(child_thread, stack + STACK_SIZE, 0, NULL)) {                                                                                                                                                                           
    perror("clone failed:");
}   
while(waitid(P_ALL, 0, NULL, WEXITED) != 0){ 
    perror("waitid failed:");
    sleep(1);
}   

手册中说:

但实际上它会立即返回:

But in fact it returns immediately :

waitid failed:: No child processes
waitid failed:: No child processes
...

有什么建议吗?

推荐答案

我不知道详细说明您要在此处完成的工作,但是通过以下方式使用waitid可能会有所帮助:

I do not know the specifics of what you are trying to get done here, but by using waitid in the following way might help:

#include <sys/types.h>
#include <sys/wait.h>

...

siginfo_t signalInfo;
waitid(P_ALL, 0, &signalInfo, WEXITED | WSTOPPED | WNOWAIT | WNOHANG);

然后在signalInfo中检查以下内容,以了解孩子退出时发生了什么事情:

Then check for the following in signalInfo to know what happened whenever child exits:

signalInfo.si_signo : For Signal Number
signalInfo.si_code : Usually SIGCHLD
signalInfo.si_errno) : Any error code set
signalInfo.si_status : For exit code of the child code

的退出代码注:使用WNOWAIT可以保留操作系统子进程的资源使用情况即使被杀死也是如此。您可能会/可能不会使用此选项。如果这样做,您将不得不在没有WNOWAIT选项的情况下再次在子对象上显式调用waitid。

Note: Using WNOWAIT makes the OS preserve the child process resource usage even after it is killed. You may/may not use this option. If you do, you will have to explicitly call waitid on the child again without the WNOWAIT option.

参考:有关此内容的更多信息,请参见手册页。

Reference: See man pages for waitid for more information on this.

这篇关于为什么waitid直到孩子终止都不会阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 06:52