使用 boost 创建具有自定义环境的子进程
Create child process with custom environment using boost
文档 boost 没有提供任何使用 process::child(...)
.
创建带有自定义环境的子进程的示例
process::system(...)
给出了一个示例,但是函数 system
的操作可能较少(例如管道或 waitpid),因此如果可能的话,我想使用 process::child
有一个完整的示例。
在system.hpp、system_impl中支持自定义env,是在child方面实现的,
template<typename IoService, typename ...Args>
inline int system_impl(
std::true_type, /*needs ios*/
std::true_type, /*has io_context*/
Args && ...args)
{
IoService & ios = ::boost::process::detail::get_io_context_var(args...);
system_impl_success_check check;
std::atomic_bool exited{false};
child c(std::forward<Args>(args)...,
check,
::boost::process::on_exit(
[&](int, const std::error_code&)
{
ios.post([&]{exited.store(true);});
}));
if (!c.valid() || !check.succeeded)
return -1;
while (!exited.load())
ios.poll();
return c.exit_code();
}
因此从文档调用系统:
bp::system("stuff", bp::env["VALUE_1"]="foo", bp::env["VALUE_2"]+={"bar1", "bar2"});
调用:
template<typename ...Args>
inline int system(Args && ...args)
{
typedef typename ::boost::process::detail::needs_io_context<Args...>::type
need_ios;
typedef typename ::boost::process::detail::has_io_context<Args...>::type
has_ios;
return ::boost::process::detail::system_impl<boost::asio::io_context>(
need_ios(), has_ios(),
std::forward<Args>(args)...);
}
翻译成这个函数,所以你应该也能做到。
最后的答案和评论很旧,但我可以确认 boost::process::child
和环境参数在 Ubuntu 18.04 下使用 Boost 1.65 版有效。关于那个的文档很薄,所以我必须自己找出来:
std::string command = "/usr/bin/something";
ipstream pipe_stream;
// Get current env
auto env = boost::this_process::environment();
// Add something
env["CHINESE_FOOD"] = "GOOD";
// Change something
env["CHINESE_FOOD"] = "GREAT";
// Delete something
env["CHINESE_FOOD"].clear();
boost::process::child childProc(command, env, std_out > pipe_stream);
当然,如果不需要环境,会自动继承父进程
std::string command = "/usr/bin/something";
ipstream pipe_stream;
boost::process::child childProc(command, std_out > pipe_stream);
文档 boost 没有提供任何使用 process::child(...)
.
创建带有自定义环境的子进程的示例
process::system(...)
给出了一个示例,但是函数 system
的操作可能较少(例如管道或 waitpid),因此如果可能的话,我想使用 process::child
有一个完整的示例。
在system.hpp、system_impl中支持自定义env,是在child方面实现的,
template<typename IoService, typename ...Args>
inline int system_impl(
std::true_type, /*needs ios*/
std::true_type, /*has io_context*/
Args && ...args)
{
IoService & ios = ::boost::process::detail::get_io_context_var(args...);
system_impl_success_check check;
std::atomic_bool exited{false};
child c(std::forward<Args>(args)...,
check,
::boost::process::on_exit(
[&](int, const std::error_code&)
{
ios.post([&]{exited.store(true);});
}));
if (!c.valid() || !check.succeeded)
return -1;
while (!exited.load())
ios.poll();
return c.exit_code();
}
因此从文档调用系统:
bp::system("stuff", bp::env["VALUE_1"]="foo", bp::env["VALUE_2"]+={"bar1", "bar2"});
调用:
template<typename ...Args>
inline int system(Args && ...args)
{
typedef typename ::boost::process::detail::needs_io_context<Args...>::type
need_ios;
typedef typename ::boost::process::detail::has_io_context<Args...>::type
has_ios;
return ::boost::process::detail::system_impl<boost::asio::io_context>(
need_ios(), has_ios(),
std::forward<Args>(args)...);
}
翻译成这个函数,所以你应该也能做到。
最后的答案和评论很旧,但我可以确认 boost::process::child
和环境参数在 Ubuntu 18.04 下使用 Boost 1.65 版有效。关于那个的文档很薄,所以我必须自己找出来:
std::string command = "/usr/bin/something";
ipstream pipe_stream;
// Get current env
auto env = boost::this_process::environment();
// Add something
env["CHINESE_FOOD"] = "GOOD";
// Change something
env["CHINESE_FOOD"] = "GREAT";
// Delete something
env["CHINESE_FOOD"].clear();
boost::process::child childProc(command, env, std_out > pipe_stream);
当然,如果不需要环境,会自动继承父进程
std::string command = "/usr/bin/something";
ipstream pipe_stream;
boost::process::child childProc(command, std_out > pipe_stream);