4

シェル コマンドを実行して終了コードを返す関数を C++ で実装しようとしていstdoutますstderr.Boost process library

std::vector<std::string> read_outline(std::string & file)
{
    bp::ipstream is; //reading pipe-stream
    bp::child c(bp::search_path("nm"), file, bp::std_out > is);

    std::vector<std::string> data;
    std::string line;

    while (c.running() && std::getline(is, line) && !line.empty())
        data.push_back(line);

    c.wait();

    return data;
}

上記のブーストのウェブサイトのでは、while ループで条件 c.running() がチェックされます。while ループに到達する前にプロセスの実行が終了した場合はどうなるでしょうか。その場合、子プロセスの標準出力をデータに保存できません。Boostのドキュメントには、次のことも記載 されています

[警告] 警告 nm 終了後に読み込もうとするとパイプがデッドロックします

したがって、 c.running() のチェックは while ループにあるはずです。

プログラムが while ループに到達する前に実行を終了したプロセスから stdout (および stderr) を取得するにはどうすればよいですか?

4

1 に答える 1