Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从C+;中长时间运行的子进程中获取输出+; 从C++程序中,我想: 启动子进程 等待,直到它发出一行输出 捕获该输出行,并允许子进程继续运行_C++_Child Process_Boost Process - Fatal编程技术网

如何从C+;中长时间运行的子进程中获取输出+; 从C++程序中,我想: 启动子进程 等待,直到它发出一行输出 捕获该输出行,并允许子进程继续运行

如何从C+;中长时间运行的子进程中获取输出+; 从C++程序中,我想: 启动子进程 等待,直到它发出一行输出 捕获该输出行,并允许子进程继续运行,c++,child-process,boost-process,C++,Child Process,Boost Process,这感觉应该很简单,但我现在尝试了两种方法,每次都遇到了类似的障碍。首先,我使用了boost::process,如下所示: auto exe = boost::process::search_path("other_executable"); boost::process::ipstream output; master_process_ = boost::process::child(exe, boost::process::std_out > output); std

这感觉应该很简单,但我现在尝试了两种方法,每次都遇到了类似的障碍。首先,我使用了
boost::process
,如下所示:

auto exe = boost::process::search_path("other_executable");
boost::process::ipstream output;
master_process_ = boost::process::child(exe, boost::process::std_out > output);
std::string important_line;
std::getline(output, important_line);
第二种方法是只使用
popen

FILE* master_process_ = popen("other_executable", "r");
char stdout_buffer[128];
while (fgets(stdout_buffer, sizeof(stdout_buffer), master_process_)) {
   // log print here
}
在这两种情况下,程序在尝试从管道中读取时都会阻塞,分别位于
getline
fgets
中,gdb显示它卡在低级读取功能中:

#0  0x00007ffff7dcc332 in __libc_read (fd=3, buf=0x555555581d40, nbytes=895) at ../sysdeps/unix/sysv/linux/read.c:26
#1  0x00007ffff794052c in read (__nbytes=<optimized out>, __buf=<optimized out>, __fd=<optimized out>) at /usr/include/x86_64-linux-gnu/bits/unistd.h:44
#2  boost::process::detail::posix::basic_pipe<char, std::char_traits<char> >::read (count=<optimized out>, data=<optimized out>, this=0x7fffffffcbd0)
    at /usr/include/boost/process/detail/posix/basic_pipe.hpp:93
#3  boost::process::basic_pipebuf<char, std::char_traits<char> >::underflow (this=0x7fffffffcb90) at /usr/include/boost/process/pipe.hpp:202
#4  0x00007ffff7c2351a in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) () from /lib/x86_64-linux-gnu/libstdc++.so.6
。/sysdeps/unix/sysv/linux/read.c:26处读取(fd=3,buf=0x5555581d40,nbytes=895)中的0 0x00007ff7dcc332
#在/usr/include/x86_64-linux-gnu/bits/unistd.h:44处读取1 0x00007FF794052C(_nbytes=,_buf=,_fd=)
#2 boost::process::detail::posix::basic_pipe::read(count=,data=,this=0x7fffffffcbd0)
at/usr/include/boost/process/detail/posix/basic_pipe.hpp:93
#3 boost::process::basic_pipebuf::underflow(this=0x7fffffffcb90)at/usr/include/boost/process/pipe.hpp:202
#4 0x00007FF7C2351A在/lib/x86\u 64-linux-gnu/libstdc++.so.6的std::basic_istream&std::getline(std::basic_istream&,std::_cx11::basic_string&,char)中
在进一步的上下文中,
other_executable
是一个小的Python程序,当它自己运行时,它的行为与预期的一样(发出输出,保持不变)。它还打开了一个服务器,在这两种情况下我都可以很好地与它通信,因此它肯定正在运行(GDB的detach消息进一步证实了这一点,正如
ps


我做错了什么?

结果表明两种实现都工作正常-问题是Python脚本没有刷新标准输出。我认为我在这里没有问题,因为我发出了一个
\n
,当从终端调用时,它似乎很好,但显然不是


感谢您在这方面的指导,@paul sanders。

这两个实现都工作得很好-问题是Python脚本没有刷新stdout。我认为我在这里没有问题,因为我发出了一个
\n
,当从终端调用时,它似乎很好,但显然不是


谢谢你的指点,@paul sanders.

其他可执行文件写入后是否刷新
stdout
?这就解决了问题,谢谢!写入后,其他可执行文件是否会刷新标准输出?这就解决了问题,谢谢!