簡體   English   中英

使用boost進程獲取stdout shell命令

[英]Get stdout of a shell command using boost process

我試圖在C ++中實現一個運行shell命令的函數,並返回退出代碼, stdoutstderr. 我正在使用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;
}

在boost的網站上面的示例中 ,在while循環中檢查條件c.running()。 如果進程在到達while循環之前完成執行該怎么辦? 在這種情況下,我將無法將子進程的stdout存儲到數據中。 Boost的文檔還提到了以下內容

[警告]警告如果您在nm退出后嘗試讀取,則管道將導致死鎖

因此,看起來c.running()的檢查應該在while循環中。

在程序到達while循環之前,如何從完成運行的進程中獲取stdout(和stderr)?

我相信等待電話是什么意思。 在此之前的任何操作系統中,子進程實際上並沒有消失(它只是在狀態未運行后才更改狀態)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM