Boost.Process - 如何使一个进程运行一个函数? [英] Boost.Process - how to make a process run a function?

查看:3839
本文介绍了Boost.Process - 如何使一个进程运行一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用 Boost.Process 进行操作,但它没有

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 

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

namespace bp = :: boost :: process;

void Hello()
{
// ...内容对我来说并不重要 - 我只想使用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;
}

#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; }



如何创建执行Hello()函数? p>

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

推荐答案

你不能。另一个进程是另一个可执行文件。除非你生成另一个相同程序的实例,子进程甚至不会包含Hello()函数。

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.

如果子进程是程序的另一个实例,定义自己的方式告诉孩子运行Hello()。这可能是进程参数或std:cin上的一些协议(即使用标准输入进行进程间通信)

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)

在UNIX / Linux平台上,您可以启动另一个进程不运行不同的可执行文件。看到fork(2)系统调用。然后你可以在孩子中调用Hello()。但是boost :: process:launch(9映射到这样的平台上的fork + exec。Plain fork()没有被boost暴露,例如因为它在其他平台上不存在。

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.

这篇关于Boost.Process - 如何使一个进程运行一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆