C++ Boost::按id处理子进程

C++ Boost::按id处理子进程,c++,boost,boost-process,C++,Boost,Boost Process,如何在on_exit函数中获取child.id() bp::child c(args, ios, bp::on_exit([&](int e, std::error_code ec) { result = e; ios.stop(); //need c.id(); })); 或者如果子函数是按id运行的,我如何签入其他函数 boost::process::child c(d

如何在on_exit函数中获取child.id()

bp::child c(args, ios, bp::on_exit([&](int e, std::error_code ec) {
            result = e;
            ios.stop();
            //need c.id();    

        }));
或者如果子函数是按id运行的,我如何签入其他函数

        boost::process::child c(data->id); // doesn't work
        if (!c.running()) {
}

您可以将任何需要的额外信息绑定到处理程序。例如,您可以声明处理程序引用
子实例

static void exit_handler(bp::child& process, int e, std::error_code ec) {
    std::cout << "Process " << process.id() << " exited with " << e << " (" << ec.message() << ")\n";
}
生成11个子进程的完整示例,这些子进程需要不同的时间<1秒才能完成,并且所有子进程都绑定到同一个处理程序:

#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iostream>
#include <functional> // std::bind & placeholders
#include <sstream>    // ostringstream

namespace bp = boost::process;
using namespace std::placeholders;
using sv = std::vector<std::string>;

static void exit_handler(bp::child& process, int e, std::error_code ec) {
    std::cout << "Process " << process.id() << " exited with " << e << " (" << ec.message() << ")\n";
}

int main() {
    boost::asio::io_context io;
    auto work = make_work_guard(io);

    std::list<bp::child> children;

    for (auto ch = 'a'; ch < 'k'; ++ch) {
        auto& p = children.emplace_back();
        std::ostringstream command;
        command << "echo 'hello from " << ch << "';";
        command << "sleep 0.$RANDOM;";
        command << "echo 'bye from " << ch << "';";
        command << "exit " << (rand()%42) << ";";

        p = bp::child(
                sv{"/bin/bash", "-c", command.str()},
                bp::on_exit(std::bind(exit_handler, std::ref(p), _1, _2)),
                io);
    }

    work.reset(); // allow io to be finished
    io.run();     // wait for that

    std::cout << "Bye\n";
}
std::map<char, bp::child> children;

for (char name = 'a'; name < 'k'; ++name) {
    std::ostringstream command;
    command << "echo 'hello from " << name << "';";
    command << "sleep " << (rand()%900 + 101)/1000.0 << ";";
    command << "echo 'bye from " << name << "';";
    command << "exit " << (rand()%42) << ";";

    auto& p = children[name];
    p = bp::child(
            sv{"/bin/sh", "-c", command.str()},
            bp::on_exit(std::bind(exit_handler, std::ref(p), _1, _2)),
            io);
}

work.reset(); // allow io to be finished

while (io.run_one()) { // wait for that
    std::cout << "Still running: ";
    for (auto& [name, child] : children) {
        if (child.running())
            std::cout << " " << name;
    }
    std::cout << std::endl;
}

std::cout << "Bye\n";
更新

还可以查询哪一个孩子还在运行,考虑使用<代码> map <代码>而不是<代码>列表 >(对容器选择的引用稳定性非常小心!) 这里有一个演示

#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iostream>
#include <functional> // std::bind & placeholders
#include <sstream>    // ostringstream

namespace bp = boost::process;
using namespace std::placeholders;
using sv = std::vector<std::string>;

static void exit_handler(bp::child& process, int e, std::error_code ec) {
    std::cout << "Process " << process.id() << " exited with " << e << " (" << ec.message() << ")\n";
}

int main() {
    boost::asio::io_context io;
    auto work = make_work_guard(io);

    std::list<bp::child> children;

    for (auto ch = 'a'; ch < 'k'; ++ch) {
        auto& p = children.emplace_back();
        std::ostringstream command;
        command << "echo 'hello from " << ch << "';";
        command << "sleep 0.$RANDOM;";
        command << "echo 'bye from " << ch << "';";
        command << "exit " << (rand()%42) << ";";

        p = bp::child(
                sv{"/bin/bash", "-c", command.str()},
                bp::on_exit(std::bind(exit_handler, std::ref(p), _1, _2)),
                io);
    }

    work.reset(); // allow io to be finished
    io.run();     // wait for that

    std::cout << "Bye\n";
}
std::map<char, bp::child> children;

for (char name = 'a'; name < 'k'; ++name) {
    std::ostringstream command;
    command << "echo 'hello from " << name << "';";
    command << "sleep " << (rand()%900 + 101)/1000.0 << ";";
    command << "echo 'bye from " << name << "';";
    command << "exit " << (rand()%42) << ";";

    auto& p = children[name];
    p = bp::child(
            sv{"/bin/sh", "-c", command.str()},
            bp::on_exit(std::bind(exit_handler, std::ref(p), _1, _2)),
            io);
}

work.reset(); // allow io to be finished

while (io.run_one()) { // wait for that
    std::cout << "Still running: ";
    for (auto& [name, child] : children) {
        if (child.running())
            std::cout << " " << name;
    }
    std::cout << std::endl;
}

std::cout << "Bye\n";

添加了一个更新,该更新还回答了问题的第二部分(或者,如果孩子正在按id运行,我如何签入其他函数?)。当然,我现在通过一些选择的id(“名称”)进行查找,但是如果需要的话,您也可以通过PID进行查找。
hello from a
hello from b
hello from c
hello from d
hello from e
hello from f
hello from g
hello from h
hello from i
Still running:  a b c d e f g h i j
Still running:  a b c d e f g h i j
hello from j
bye from i
Still running:  a b c d e f g h j
Process 30748 exited with -1
Still running:  a b c d e f g h j
bye from e
Still running:  a b c d f g h j
Still running:  a b c d f g h j
Process 30739 exited with -1
Still running:  a b c d f g h j
bye from c
Still running:  a b d f g h j
Still running:  a b d f g h j
Process 30735 exited with -1
Still running:  a b d f g h j
bye from b
Still running:  a d f g h j
Still running:  a d f g h j
Process 30733 exited with -1
Still running:  a d f g h j
bye from h
Still running:  a d f g j
Still running:  a d f g j
Process 30744 exited with -1
Still running:  a d f g j
bye from d
Still running:  a f g j
Still running:  a f g j
Process 30737 exited with -1
Still running:  a f g j
bye from g
Still running:  a f j
Still running:  a f j
Process 30743 exited with -1
Still running:  a f j
bye from f
Still running:  a j
Still running:  a j
Process 30740 exited with -1
Still running:  a j
bye from j
Still running:  a
Still running:  a
Process 30749 exited with -1
Still running:  a
bye from a
Still running: 
Still running: 
Process 30732 exited with -1
Still running: 
Bye