C++输入和输出管道到外部程序

标签 c++ process pipe system

我正在尝试使用一些输入调用外部程序并在程序中检索输出。

它看起来像;

(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/

相关文章:

process - Yii框架实际开发流程

Angular:使用搜索输入和特定过滤器选项过滤表格内容

c++ - 如何分配给结构对象的成员?

c++ - Visual Studio C++ 多项目解决方案

ruby - 为什么 Process.setsid 在 ruby​​ 中需要 root 权限?

asynchronous - 使用 Proc::Async 从绑定(bind)管道中读取

c++ - 在堆栈上初始化多态变量

c++ - 在 C++ 中设置 Windows 系统时间

bash - 打印用 grep 找到的正则表达式字符串

c - 用 C 语言构建全双工管道