我有一个线程启动了一个boost::process应用程序,代码如下:
void Service::Run()
{
printf("Starting thread for service ID %i\n", 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: %i\n", 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: %i\n", exit_code);
}
catch(boost::system::system_error err)
{
printf("Something went wrong: %s\n", err.what());
}
this->manager->ServiceDie(this->sid);
return;
}
由于这个函数是阻塞的,它会等待服务被杀死(外部或者我需要通过标准输入来优雅地停止应用程序)。
我不知道如何写入到子进程的标准输入。我已经尝试过这样:
*(this->stdIn) << "stop\n";
在另一个线程(
Manager
类)中调用的Service
类中的公共函数内部。然而,这并没有产生任何结果。我该如何写入子进程
proc
的标准输入?
get_stdin()
函数...不确定为什么,哦,我也没有launch()
。 - Zinglish