我正在尝试使用一些输入调用外部程序并在程序中检索输出。
它看起来像;
(some input) | (external program) | (retrieve output)
我首先考虑使用 popen()
,但似乎不可能,因为管道不是双向的。
在 linux 中有没有简单的方法来处理这类问题?
我可以尝试制作一个临时文件,但如果它可以在不访问磁盘的情况下清楚地处理,那就太好了。
有什么解决办法吗?谢谢。
最佳答案
在 Linux 上你可以使用 pipe
功能:打开两个新管道,每个方向一个,然后使用 fork
创建一个子进程,之后,您通常会关闭未使用的文件描述符(在父级上读取结束,在管道的子级上写入结束,以便父级发送给子级,反之亦然),然后使用 execve
启动您的应用程序或其前端之一。
如果你dup2管道的文件描述符到标准控制台文件句柄(STDIN_FILENO
/STDOUT_FILENO
;每个进程单独),您应该甚至能够使用 std::cin
/std::cout
用于与其他进程通信(您可能只想为子进程这样做,因为您可能希望将控制台保留在父进程中).不过,我还没有对此进行测试,所以这就留给你了。
完成后,您还 wait
or waitpid
让您的子进程终止。可能类似于以下代码:
int pipeP2C[2], pipeC2P[2];
// (names: short for pipe for X (writing) to Y with P == parent, C == child)
if(pipe(pipeP2C) != 0 || pipe(pipeC2P) != 0)
{
// error
// TODO: appropriate handling
}
else
{
int pid = fork();
if(pid < 0)
{
// error
// TODO: appropriate handling
}
else if(pid > 0)
{
// parent
// close unused ends:
close(pipeP2C[0]); // read end
close(pipeC2P[1]); // write end
// use pipes to communicate with child...
int status;
waitpid(pid, &status, 0);
// cleanup or do whatever you want to do afterwards...
}
else
{
// child
close(pipeP2C[1]); // write end
close(pipeC2P[0]); // read end
dup2(pipeP2C[0], STDIN_FILENO);
dup2(pipeC2P[1], STDOUT_FILENO);
// you should be able now to close the two remaining
// pipe file desciptors as well as you dup'ed them already
// (confirmed that it is working)
close(pipeP2C[0]);
close(pipeC2P[1]);
execve(/*...*/); // won't return - but you should now be able to
// use stdin/stdout to communicate with parent
}
}
关于C++输入和输出管道到外部程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51996946/