提升::p Rocess 写入 stdin
boost::process write to stdin
我有一个线程启动了一个 boost::p rocess 应用程序,线程运行的代码如下:
void Service::Run()
{
printf("Starting thread for service ID %in", this->sid);
// Set up a stream sink for stdout, stderr and stdin
bprocess::pipe pstdIn = create_pipe();
bprocess::pipe pstdOut = create_pipe();
file_descriptor_sink fdSink(pstdIn.sink, close_handle);
file_descriptor_source fdSrc(pstdOut.sink, close_handle);
// Set up the read write streams
this->stdIn = new fdistream(fdSink);
this->stdOut = new fdostream(fdSrc);
// Execute the service
try
{
child proc = execute(
set_args(this->args),
inherit_env(),
bind_stdin(fdSrc),
throw_on_error()
);
printf("PID: %in", proc.pid);
// Wait for the application to end then call a cleanup function
int exit_code = wait_for_exit(proc);
printf("Service died, error code: %in", exit_code);
}
catch(boost::system::system_error err)
{
printf("Something went wrong: %sn", err.what());
}
this->manager->ServiceDie(this->sid);
return;
}
由于此函数是一个阻塞函数,它本质上是等待服务被终止(外部或根据需要;通过 stdin 输入以优雅地停止应用程序)。
我不知道如何写入子进程的 stdin。我试过这个:
*(this->stdIn) << "stopn";
在另一个线程(Manager
类)中调用的 Service
类中的公共函数内。然而,这没有结果。
我怎样才能写到子proc
的标准
下面是一个改编自此处示例的演示:
你可以看到它在Coliru Live上,可悲的是,这正在挑战Coliru的极限。可能更晚:
#include <boost/process.hpp>
#include <boost/assign/list_of.hpp>
#include <string>
#include <vector>
#include <iostream>
using namespace boost::process;
int main()
{
std::string exec = find_executable_in_path("rev");
std::vector<std::string> args = boost::assign::list_of("rev");
context ctx;
ctx.environment = self::get_environment();
ctx.stdin_behavior = capture_stream();
ctx.stdout_behavior = capture_stream();
child c = launch(exec, args, ctx);
postream &os = c.get_stdin();
pistream &is = c.get_stdout();
os << "somencookiesnforntasting";
os.close();
std::cout << is.rdbuf();
}
输出:
emos
seikooc
rof
gnitsat
现在,如果你需要它真正的异步,还有另一个使用Boost Asio的示例在该页面的下方
相关文章:
- 写入向量<向量<bool>>
- 如何将内容数组写入文本文件?
- 写入位置0x0000000C时发生访问冲突
- 使用ios:ate写入到流会覆盖现有文件
- 独立读取-修改-写入顺序
- 为什么在某些情况下不写入此文件?
- 用C++将哈希表写入文件并从文件中恢复
- std::当在256字节边界上写入整数时,流的奇怪行为
- 为什么指针不写入类的地址?
- 如何打开并写入一个名称取自C++中字符串的文件
- 如何在C++中从文件中读取/写入多个对象
- 将类型化数组写入子进程 stdin 无法正常工作
- 如何将数据写入 stdin,以便由等待来自 stdin 输入的单独线程使用?
- 提升::p Rocess 写入 stdin
- C++:在数据写入管道时从 stdin 读取
- 通过 Windows 管道写入进程 STDIN
- 如何在c++中运行应用程序,并在windows中向其stdin写入和从其stdout读取
- c++写入stdin(在Windows中)
- 写入STDIN以使用其PID进行处理
- 如何执行和写入stdin并将stdout管道连接到套接字