c++ - 启动子进程中的竞争条件导致从管道读取挂起

标签 c++ linux boost boost-process

我有两个线程,每个线程启动一个子进程。第一个应用程序是一个运行时间很长的二进制文件。第二个退出很快。

存在竞争条件,有时会导致失败。下面我有一个最小可行的代码示例。

它使用 Boost Process 0.5,它使用标准的 fork/execve/dup2 系统。关于 Boost Process 的工作原理有一些 hack,但总的来说它工作得很好。

父进程启动了更多的进程,并且通常它是有效的。

我无法轻松地一次启动一个进程,尤其是因为我不知道哪些部分不能交错。

关于为什么会挂起的任何想法?

预期输出:

/etc/init.d/led restart: Creating child
Creating child1
Reading STDOUT
/etc/init.d/led restart: Waiting for it to exit
Reading std_err_pipe


wait_for_exit(pullapp);
Reading std_out_pipe
< file list>

Done

但是,它通常(但并非总是)在 std_err_pipe 处停止。

#include <iostream>
#include <string>
#include <vector>

#include <boost/iostreams/stream.hpp>
#include <boost/process.hpp>
#include <boost/thread.hpp>

void run_sleep()
{
    int      exit_code;
    std::string   str;

    std::vector< std::string >  args;

    boost::shared_ptr<boost::process::child> child;

    args.push_back(boost::process::search_path("sleep"));
    args.push_back("20");

    boost::iostreams::stream< boost::iostreams::file_descriptor_source >
        out_stream;

    boost::process::pipe out_pipe = boost::process::create_pipe();

    {
        //MUST BE IN SEPARATE SCOPE SO SINK AND SOURCE ARE DESTROYED
        // See http://stackoverflow.com/a/12469478/5151127
        boost::iostreams::file_descriptor_sink    out_sink
            (out_pipe.sink,   boost::iostreams::close_handle);
        boost::iostreams::file_descriptor_source  out_source
            (out_pipe.source, boost::iostreams::close_handle);

        std::cout << "Creating child1" << std::endl;
        child.reset(new boost::process::child(
            boost::process::execute(
                boost::process::initializers::run_exe(args[0]),
                boost::process::initializers::set_args(args),
                boost::process::initializers::bind_stdout(out_sink),
                boost::process::initializers::bind_stderr(out_sink)
            )
        ));

        out_stream.open(out_source);
    }

    std::cout << "Reading STDOUT" << std::endl;
    while( out_stream ) {
        std::string line;

        std::getline(out_stream, line);

        std::cout << line << std::endl;
    }

    std::cout << "wait_for_exit(pullapp);" << std::endl;
    exit_code = wait_for_exit(*child);

    child.reset();

    return;
}


void run_ls()
{
    int      exit_code;
    std::string   str;

    std::vector< std::string >  args ;

    args.push_back(boost::process::search_path("ls"));
    args.push_back("/lib");

    boost::process::pipe std_out_pipe = boost::process::create_pipe();
    boost::process::pipe std_err_pipe = boost::process::create_pipe();

    std::cout << "/etc/init.d/led restart: Creating child" << std::endl;

    {
        boost::process::child child = boost::process::execute(
            boost::process::initializers::set_args(args),
            boost::process::initializers::bind_stdout(
                boost::iostreams::file_descriptor_sink(
                    std_out_pipe.sink,
                    boost::iostreams::close_handle
                )
            ),
            boost::process::initializers::bind_stderr(
                boost::iostreams::file_descriptor_sink(
                    std_err_pipe.sink,
                    boost::iostreams::close_handle
                )
            ),
            boost::process::initializers::throw_on_error()
        );

        std::cout << "/etc/init.d/led restart: Waiting for it to exit" << std::endl;
        exit_code = wait_for_exit(child);
    }

    { //with std_err_stream, istream
        boost::iostreams::stream< boost::iostreams::file_descriptor_source >
            std_err_stream(
                boost::iostreams::file_descriptor_source(
                    std_err_pipe.source, boost::iostreams::close_handle
                )
            );

        std::cout << "Reading std_err_pipe" << std::endl;
        std::istream istream(std_err_stream.rdbuf());
        while( istream ) {
            getline(istream, str);
            std::cout << str << std::endl;
        }
    }

    { //with std_out_stream, istream
        boost::iostreams::stream< boost::iostreams::file_descriptor_source >
            std_out_stream(
                boost::iostreams::file_descriptor_source(
                    std_out_pipe.source, boost::iostreams::close_handle
                )
            );

        std::cout << "Reading std_out_pipe" << std::endl;
        std::istream istream(std_out_stream.rdbuf());
        while( istream ) {
            getline(istream, str);
            std::cout << str << std::endl;
        }
    }

    std::cout << "Done" << std::endl;
}

int main()
{
    boost::thread  run_sleep_tr(run_sleep);
    boost::thread  run_ls_tr(run_ls);

    run_sleep_tr.join();
    run_ls_tr.join();

    return 0;
}

(另存为 process-test.cpp 并使用 g++ process-test.cpp -o process-test -lboost_iostreams -lboost_filesystem -lboost_thread -lboost_system 编译)

最佳答案

显然这是因为文件句柄在多个进程中结束。这些进程不会关闭那些句柄,因此父进程仍在等待。

对于 Linux,修复相对容易;管道应使用 create_pipe 中的 O_CLOEXEC 创建。 bind_* 方法中的 dup2 调用会清除此标志,这足以让管道正常工作。

在 Windows 上,我还没有真正找到解决方案。您必须将句柄标记为可继承。可以在 executor() 方法中执行此操作,但这可能需要全局互斥锁。我还没有时间仔细研究它。

关于c++ - 启动子进程中的竞争条件导致从管道读取挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42117551/

相关文章:

c++ - 如何将成员函数作为回调参数传递给需要 `typedef-ed` 自由函数指针的函数?

c++ - boost range for_each、bind、copy 和 back_inserter 的组合失败

c++ - 在 Windows 中无法从 cmake 中找到 MySQL 和 Boost includes/libs

C++ 使用 Boost.asio 和 Beast 库在正文中发送数据

c++ - 为一个 C++ vector 成员添加一个标志

c++ - 无法使用 Microsoft Visual Studio C++ 2008 速成版编译 C 代码

regex - grep 前 n 行,只返回文件名

linux - 删除列名时 SED 命令错误

c - windows linux bash 上的 GCC

c++ - 在C++中与libmongoclient链接时出错