1

プロセスが終了したかどうかを確認することは可能ですか?

を呼び出したくありません.wait()。ブロックしているためです。また、タイムアウトを管理したいので、プロセスを終了します。

今、私は次のコードを持っています:

child c = launch(exec, args, ctx);
auto timeout = boost::posix_time::seconds(3);
boost::this_thread::sleep(timeout);
c.terminate();

ただし、終了を待たず、プロセスが正常に終了したかどうかを確認しません。

4

1 に答える 1

2

これは 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

または、C++11 が利用可能な場合は、boost.process 0.6 を使用してください。

于 2016-09-02T16:34:06.247 に答える