提升进程运行速度

时间:2018-10-17 15:18:18

标签: c++ macos boost boost-asio

我使用boost 1_65_1并尝试在MacOS上运行一个进程。 我的主要流程是作为root运行 我需要以普通用户身份启动子启动。 我的命令行: su user_name -c“完整的可执行文件路径”。 当我以root身份在终端中运行命令行时,就可以正常工作。

当我尝试使用Boost Process运行它时,进程“ su”正在运行,但立即退出并出现错误2。

我运行流程的代码:

void RunProcess(std::string a_PathToExe, std::vector<std::string> a_Args, io_servicePtr a_pIoServicePtr,
                ProcessExitCallBack a_pProcessExitCallBack) {

    Error l_Error;
    std::stringstream l_ShellCommandLine;
    ProcessHandlePtr l_pProcess = NULL;
    boost::process::group l_Group;
    boost::shared_ptr<boost::process::async_pipe> l_PipeOutputStream =
        boost::shared_ptr<boost::process::async_pipe>(new boost::process::async_pipe(*(a_pIoServicePtr.get())));
    boost::shared_ptr<boost::process::async_pipe> l_PipeError =
        boost::shared_ptr<boost::process::async_pipe>(new boost::process::async_pipe(*(a_pIoServicePtr.get())));

    try {

        l_ShellCommandLine << "\"" << a_PathToExe << "\""
                           << " ";
        for (size_t i = 0; i < a_Args.size(); ++i) {
            l_ShellCommandLine << a_Args.at(i) << " ";
        }

        l_pProcess = ProcessHandlePtr(new boost::process::child(
            l_ShellCommandLine.str(), boost::process::std_in.close(),
            boost::process::std_out > *(l_PipeOutputStream.get()), boost::process::std_err > *(l_PipeError.get()),
            *(a_pIoServicePtr.get()), l_Group, boost::process::shell,
            boost::process::on_exit = [](int exit, const std::error_code &ec_in) {
                WRITE_INFO_LOG("The process exit with error %d %s", exit, ec_in.message().c_str());
            }));

        l_PID = l_pProcess->id();
        WaiteForProcessToExit(l_pProcess, l_PipeOutputStream, l_PipeError, a_pProcessExitCallBack);
    }

    catch (std::exception &e) {

    } catch (...) {
    }
    return;
}

void WaiteForProcessToExit(ProcessHandlePtr a_pProcessHandle,
                           boost::shared_ptr<boost::process::async_pipe> a_PipeOutputStream,
                           boost::shared_ptr<boost::process::async_pipe> a_PipeError,
                           ProcessExitCallBack a_pProcessExitCallBack) {

    std::array<char, 1024> buffer = std::array<char, 1024>{};
    a_PipeOutputStream->async_read_some(
        boost::process::buffer(buffer), [=](boost::system::error_code ec, size_t transferre) {
            if (transferre > 0) {
                std::string Line = std::string(buffer.data());
                WRITE_INFO_LOG("%s", Line.c_str());
            }

            if (ec != 0) {

                WRITE_INFO_LOG("The Process %d has exit with error code %d", a_pProcessHandle->id(), ec);
                if (a_pProcessExitCallBack != NULL) {
                    a_pProcessExitCallBack(a_pProcessHandle->id(), Error());
                }
            } else {
                WaiteForProcessToExit(a_pProcessHandle, a_PipeOutputStream, a_PipeError, a_pProcessExitCallBack);
            }
        });
}

为什么子程序无法运行? 预先感谢

2 个答案:

答案 0 :(得分:1)

首先,您没有显示足够的代码。

第二,如果您使用su,则不需要bp::shell

第三,该代码看起来像是C编译器从中逃脱了。为什么这么复杂?为什么要进行所有动态内存管理?为什么要异步,如果您要做的就是等待完成?

这是我的简化版本,对我有用。也许您可以找出正在做的不同的查找器详细信息:

同步,C ++:

Live On Coliru

#include <iostream>
#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>

namespace bp = boost::process;

struct ProcessResult {
    std::string out, err;
    int exitcode;
};

ProcessResult RunProcess(std::string exe, std::vector<std::string> args, boost::asio::io_service& io) {
    std::future<std::string> out, err;
    bp::group group;

    bp::child child(
        bp::search_path(exe), args, bp::std_in.close(), bp::std_out > out, bp::std_err > err, io, group
    );

    child.wait();
    return { out.get(), err.get(), child.exit_code() };
}

int main() {
    boost::asio::io_service io;

    // keep work to avoid io.run to complete early
    auto work = boost::asio::make_work_guard(io);
    std::thread io_thread([&io] { io.run(); });

    auto pstree = RunProcess("su", { "sehe", "-c", "/usr/bin/free" }, io);

    std::cout << "Exitcode: " << pstree.exitcode << "\n";
    std::cout << "Output: " << pstree.out << "\n";
    std::cout << "Error: " << pstree.err << "\n";

    work.reset(); // allow service to complete
    io_thread.join();
}

打印什么(以root身份运行时):

Exitcode: 0
Output:               total        used        free      shared  buff/cache   available
Mem:       32832516    21846896     2486924     2963636     8498696     5595284
Swap:             0           0           0

Error: 

不以超级用户身份运行时:

Exitcode: 1
Output: 
Error: su: must be run from a terminal

异步

您仍然可以将异步与C ++样式结合在一起。参见例如

答案 1 :(得分:0)

谢谢您重播。

我的问题是,运行X控制台后看到的“ su”进程输出以下错误“ nw_path_close_fd无法关闭受保护的necp fd 4 [9:错误的文件描述符]”

在我的代码中,由于我的应用程序的其余部分需要运行,因此我需要使用异步。

我发现“ boost :: process :: async_system”可以完成工作,但是没有该进程的PID。