且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Boost.Process - 如何使一个进程运行一个函数?

更新时间:2021-08-26 04:35:21

你不能。另一个进程是另一个可执行文件。除非你生成另一个相同程序的实例,子进程甚至不会包含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.