Boost - 子进程仍然是僵尸
Boost - the child process remains as a zombie
我写了简单的代码,运行s 子进程是分离的:
boost::process::child childProcess
(
"sleep 10",
boost::process::std_in.close(),
boost::process::std_out.close()
);
childProcess.detach();
子程序完成后,ubuntu 中的命令“top”向我显示此条目:
root 8935 0.0 0.0 0 0 pts/0 Z 12:10 0:00 [sleep] <defunct>
有什么想法吗?
#编辑
此应用程序将在多个操作系统上 运行。我尝试了其他一些解决方案,例如线程:
std::thread childProcessThread([](){
boost::process::child childProcess
(
"sleep 10",
boost::process::std_in.close(),
boost::process::std_out.close()
);
childProcess.wait();
});
childProcessThread.detach();
有时我会收到错误消息“free()”。
这个解决方案正确吗?
保留zombie process是因为子进程终止时向父进程生成SIGCHLD
信号,但父进程不处理该信号(通过读取子进程的状态信息)。
假设父进程需要保留运行,你可以(在父进程中):
等待子进程结束(这是阻塞等待):
childProcess.wait();
为 SIGCHLD
添加一个信号处理程序(请注意,如果有多个子进程,这会有点棘手),例如。 :
void handle_sigchld(int signum)
{
wait(NULL); // or some other wait variant that reads the child process' status information
}
signal(SIGCHLD, handle_sigchld);
忽略 SIGCHLD
信号(尽管这会阻止获取任何子进程的状态信息):
signal(SIGCHLD, SIG_IGN);
daemonize子进程
我写了简单的代码,运行s 子进程是分离的:
boost::process::child childProcess
(
"sleep 10",
boost::process::std_in.close(),
boost::process::std_out.close()
);
childProcess.detach();
子程序完成后,ubuntu 中的命令“top”向我显示此条目:
root 8935 0.0 0.0 0 0 pts/0 Z 12:10 0:00 [sleep] <defunct>
有什么想法吗?
#编辑 此应用程序将在多个操作系统上 运行。我尝试了其他一些解决方案,例如线程:
std::thread childProcessThread([](){
boost::process::child childProcess
(
"sleep 10",
boost::process::std_in.close(),
boost::process::std_out.close()
);
childProcess.wait();
});
childProcessThread.detach();
有时我会收到错误消息“free()”。 这个解决方案正确吗?
保留zombie process是因为子进程终止时向父进程生成SIGCHLD
信号,但父进程不处理该信号(通过读取子进程的状态信息)。
假设父进程需要保留运行,你可以(在父进程中):
等待子进程结束(这是阻塞等待):
childProcess.wait();
为
SIGCHLD
添加一个信号处理程序(请注意,如果有多个子进程,这会有点棘手),例如。 :void handle_sigchld(int signum) { wait(NULL); // or some other wait variant that reads the child process' status information } signal(SIGCHLD, handle_sigchld);
忽略
SIGCHLD
信号(尽管这会阻止获取任何子进程的状态信息):signal(SIGCHLD, SIG_IGN);
daemonize子进程