Boost.Process - how to make a process run a function?

落花浮王杯 提交于 2019-12-01 06:13:44

问题


So I try to do something with Boost.Process though it has not been accepted into the Boost distribution yet.

simpliest programm would look like

#include <boost/process.hpp> 
#include <string> 
#include <vector> 

namespace bp = ::boost::process; 

void Hello()
{
    //... contents does not matter for me now - I just want to make a new process running this function using Boost.Process.
} 

bp::child start_child() 
{ 
    std::string exec = "bjam"; 

    std::vector<std::string> args; 
    args.push_back("--version"); 

    bp::context ctx; 
    ctx.stdout_behavior = bp::silence_stream(); 

    return bp::launch(exec, args, ctx); 
} 

int main() 
{ 
    bp::child c = start_child(); 

    bp::status s = c.wait(); 

    return s.exited() ? s.exit_status() : EXIT_FAILURE; 
} 

how to tall process I am creating to perform Hello() function?


回答1:


You can't. Another process is another executable. Unless you spawn another instance of the same program, the child process will not even contain the Hello() function.

If the child is another instance of your program, you need to define your own way to tell the child to run Hello(). That could be process arguments or some protocol on std:cin (i.e. using standard input for inter-process communication)

On a UNIX/Linux platform you can start another process and NOT run a different executable. See the fork(2) system call. Then you can call Hello() in the child. But boost::process:launch(9 map to fork+exec on such platforms. Plain fork() is not exposed by boost, for example because it doesn't exist on other platforms.

There may be extremely platform-dependent ways to do what you want, but you don't want to go there.



来源:https://stackoverflow.com/questions/4774932/boost-process-how-to-make-a-process-run-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!