在C++中,fork和kill并没有杀死所有的子进程
In C++, fork and kill not killing all the child processes
我有下面的代码,它在子进程中执行二进制,等待1秒,如果没有完成,就杀死它。
pid_t pid;
pid=fork();
if (pid == 0)
{
//In child
execl("/bin/sh", "sh", "-c", "/opt/qcom/bin/version.out > /tmp/version", (char *)NULL);
exit(0);
}
else
{
// In parent, wait for 1 second
sleep(1);
int status;
if (waitpid(pid, &status, WNOHANG) != pid)
{
//kill(pid, SIGTERM); //--> tried this too
kill(pid, SIGKILL);
}
fsmverDir("/tmp/version");
system("rm /tmp/version");
}
但它并没有完全杀死,我在运行我的程序3次后看到下面的ps输出(它创建了3个版本.out),"sh"看起来像僵尸。。。
# ps | grep "version.out|sh"
2012 root 0 Z [sh]
2013 root 13236 S /opt/qcom/bin/version.out
2058 root 0 Z [sh]
2059 root 13236 S /opt/qcom/bin/version.out
2092 root 0 Z [sh]
2093 root 13236 S /opt/qcom/bin/version.out
2100 root 2360 S grep version.out|sh
#
或者,有没有一种方法可以在busybox Linux中超时运行命令
类似于:
execlp("timeout","timeout","1","sh","-c","/opt/qcom/bin/version.out > /tmp/version",NULL);
在我的busybox版本中没有超时,有其他选择吗?
您正在创建一个shell(子shell),它反过来运行"version.out"(孙shell)。
你杀死了孩子,从而使其成为僵尸,并使孙子成为孤儿。你可以通过第二次调用wait
来收集孩子(第一次它出错了,否则你永远不会调用kill
),但你仍然无法实现从父母那里杀死孙子的目标。
相关文章:
- 试图对缓存进行跨步测试,但程序并没有结束
- 终止 QProcess 不会终止子进程
- 什么时候最好在子进程中使用 CPU 或 I/O 密集型代码 [ C++ ]
- 子进程更新共享 mmap 内存,但父进程没有更改
- 使用 waitpid 时等待子进程终止
- 如何使用 c++ libboost 运行进程并获取其输出?
- 使用 Node.js 调用child_process与从 C 调用子进程并创建C++绑定以从 node.js 调用
- 如何在 c++ 中正确使用 ifstreams,带有子进程并避免泄漏?
- async_read on async_pipe子进程没有提供数据
- 将gcov与子进程和共享库一起使用时没有覆盖范围
- 如何在没有嵌套作业对象的情况下可靠地控制子进程生存期
- 如何正确使用文件映射并将数据传递给子进程
- 仅重定向子进程stdin,并从stdout和stderr中删除数据
- 在linux中fork子进程后没有数据
- 如何将视频文件作为输入传递给NodeJS中的子进程,并接收帧/图像作为输出
- (c++, Windows)生成没有控制台窗口的子进程(使用CreateProcess)
- 在C++中,fork和kill并没有杀死所有的子进程
- 暂停并恢复 Qt 中由 QProcess 启动的子进程
- 如何在没有信号的情况下监视子进程
- 如何使子进程具有与 parrent 相同的环境变量,并在窗口中拥有自己的环境变量?