C++ 到外部程序的输入和输出管道 [英] C++ both input and output pipe to the external program

查看:47
本文介绍了C++ 到外部程序的输入和输出管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to invoke external program with some input and retrieve the output from it within a program.

看起来像;

(一些输入)|(外部程序) |(检索输出)

(some input) | (external program) | (retrieve output)

我首先考虑使用 popen() 但似乎不可能,因为 管道不是双向的.

I first thought about using a popen() but it seems like, it is not possible because the pipe is not bidirectional.

linux 中有没有什么简单的方法可以处理这种事情?

Is there any easy way to handle this kind of stuff in linux?

我可以尝试制作一个临时文件,但如果可以在不访问磁盘的情况下清楚地处理它,那就太好了.

I can try making a temp file but it will be great if it can be handled clearly without accessing the disk.

有什么解决办法吗?谢谢.

Any Solution? Thanks.

推荐答案

在 linux 上你可以使用 pipe 函数:打开两个新管道,每个方向一个,然后使用 fork,之后,您通常会关闭未使用的文件描述符(在父级上读取结束,在子级上写入结束)父管道发送给孩子,反之亦然其他管道),然后使用 execve 或其前端之一.

On linux you can use pipe function: Open two new pipes, one for each direction, then create a child process using fork, afterwards, you typically close the file descriptors not in use (read end on parent, write end on child of the pipe for parent sending to child and vice versa for the other pipe) and then start your application using execve or one of its front ends.

如果您将管道的文件描述符dup2 到标准控制台文件句柄(STDIN_FILENO/STDOUT_FILENO;每个进程分开),你应该甚至可以使用std::cin/std::cout 用于与其他进程通信(您可能只想为子进程这样做,因为您可能希望将控制台保留在父进程中).不过,我还没有对此进行过测试,所以就交给你了.

If you dup2 the pipes' file descriptors to the standard console file handles (STDIN_FILENO/STDOUT_FILENO; each process separately), you should even be able to use std::cin/std::cout for communicating with the other process (you might want to do so only for the child, as you might want to keep your console in parent). I have no tested this, though, so that's left to you.

完成后,您还需要 waitwaitpid 以便您的子进程终止.可能类似于以下代码:

When done, you'd yet wait or waitpid for your child process to terminate. Might look like similar to the following piece of code:

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++ 到外部程序的输入和输出管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆