Boost.进程检查进程是否终止
Boost.Process check if process terminated
是否可以检查进程是否终止?
我不想调用.wait()
,因为它正在阻塞,并且我想管理超时,之后我将终止该过程。
现在我有以下代码:
child c = launch(exec, args, ctx);
auto timeout = boost::posix_time::seconds(3);
boost::this_thread::sleep(timeout);
c.terminate();
但它不会等待终止,也不会检查进程是否正常启动。
这不在 boost.process 0.5 中(在 0.6 中是(,所以你需要自己实现它。可以这样完成:
#if defined (BOOST_WINDOWS_API)
template<typename Child>
inline bool is_running(const Child &p, int & exit_code)
{
DWORD code;
//single value, not needed in the winapi.
if (!GetExitCodeProcess(p.proc_info.hProcess, &code))
throw runtime_error("GetExitCodeProcess() failed");
if (code == 259) //arbitrary windows constant
return true;
else
{
exit_code = code;
return false;
}
}
#elif defined(BOOST_POSIX_API)
tempalte<typename Child>
inline bool is_running(const Child&p, int & exit_code)
{
int status;
auto ret = ::waitpid(p.pid, &status, WNOHANG|WUNTRACED);
if (ret == -1)
{
if (errno != ECHILD) //because it no child is running, than this one isn't either, obviously.
throw std::runtime_error("is_running error");
return false;
}
else if (ret == 0)
return true;
else //exited
{
if (WIFEXITED(status))
exit_code = status;
return false;
}
}
#endif
或者只使用 boost.process 0.6,如果您有 C++11 可用。
相关文章:
- 使用 waitpid 时等待子进程终止
- 检测到由于操作系统内存不足而导致子进程终止
- 终端进程终止,退出代码为:1有什么方法可以修复它吗
- 使用system("./some_program"),当"some_program"死亡时,如何防止父进程终止?
- Ubuntu 服务器管道在第一次退出时停止进程终止
- win32 进程终止检测:WMI 与 WaitForSingleObject
- 如何停止DLL中的进程终止异常
- 使用终止进程终止"mstsc.exe"进程时出现错误代码(5)访问被拒绝
- 在 Linux 上使用 C++ 记录进程终止的原因
- 可以终止线程从另一个进程终止线程
- 进程等待另一进程终止时的信号处理
- MPI::COMM::Create之后的死锁(所有进程终止)
- 代码::阻止进程终止,状态为1
- libpqxx:如何在连接进程终止后重新连接到Postgresql数据库
- Fork进程:在子进程终止时恢复父进程(Linux)
- Qt处理进程终止
- 在所有子进程终止后,不能运行父进程
- 调用函数时进程终止
- C/ c++窗口中的进程终止
- 由于c++ bool函数的StackOverflowException导致进程终止