生成的子进程退出时状态 = 127
我使用 posix_spawnp 执行不同的进程,并检查状态(使用 waitpid)以确保子进程已正确创建。
int iRet = posix_spawnp(&iPID, zPath, NULL, NULL, argv, environ);
if (iRet != 0)
{
return false;
}
int iState;
waitpid(static_cast<pid_t>(iPID), &iState, WNOHANG);
cout << "Wait: PID " << iPID << " | State " << iState << endl;
if (WIFEXITED(iState)) {
printf("Child exited with RC=%d\n",WEXITSTATUS(iState));
}
else if (WIFSIGNALED(iState)) {
printf("Child exited via signal %d\n",WTERMSIG(iState));
}
else
{
printf("Child is NORMAL");
}
首先,它正确执行,我收到以下消息:
等待:PID 15911 | 状态 0 子进程退出 RC=0
执行同一进程多次后,子进程开始退出,状态为 127。
等待:PID 15947 | 州 32512 儿童 退出时 RC=127
发生这种情况后,我无法让孩子再次生成。 我将上面给出的代码部分包含在 for 循环中,但它无法正确生成。 如果我重新启动父进程,它会工作一段时间,但过了一会儿又会出现同样的问题。
我在这里做错了什么?
I use posix_spawnp to execute different processes and I check the status (with waitpid) to make sure the child was created properly
int iRet = posix_spawnp(&iPID, zPath, NULL, NULL, argv, environ);
if (iRet != 0)
{
return false;
}
int iState;
waitpid(static_cast<pid_t>(iPID), &iState, WNOHANG);
cout << "Wait: PID " << iPID << " | State " << iState << endl;
if (WIFEXITED(iState)) {
printf("Child exited with RC=%d\n",WEXITSTATUS(iState));
}
else if (WIFSIGNALED(iState)) {
printf("Child exited via signal %d\n",WTERMSIG(iState));
}
else
{
printf("Child is NORMAL");
}
At first this executes properly and I get the following message:
Wait: PID 15911 | State 0 Child exited
with RC=0
After executing the same process several times, the child process starts to exit with status 127.
Wait: PID 15947 | State 32512 Child
exited with RC=127
After this happens, I could not get the child to spawn again. I enclosed the section of code given above in a for loop but it wouldn't spawn properly.
If I restart the parent process, it works for a while but the same problem crops up again after a while.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查
例如:
看起来它可能会由于多种原因以 127 退出。
Check this link.
For example:
It looks as if it might exit with 127 for a whole host of reasons.
检查
waitpid()
的返回码以确保没有问题。代码的读取方式表明您一次仅生成一个子进程(否则无需在循环内调用
waitpid()
)。 但在这种情况下,我不会期望使用 WNOHANG。Check the return code from
waitpid()
to be sure that it isn't having problems.The way the code reads suggests that you are only spawning one child process at a time (otherwise there'd be no need to call
waitpid()
within the loop). However in that case I wouldn't expect to useWNOHANG
.