简体   繁体   English

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

[英]C++ both input and output pipe to the external program

I am trying to invoke external program with some input and retrieve the output from it within a program. 我试图用一些输入调用外部程序,并在程序中检索它的输出。

It will be look like; 看起来像;

(some input) | (一些输入)| (external program) | (外部程序)| (retrieve output) (检索输出)

I first thought about using a popen() but it seems like, it is not possible because the pipe is not bidirectional . 我首先考虑使用popen()但似乎,这是不可能的,因为管道不是双向的

Is there any easy way to handle this kind of stuff in linux ? 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. 谢谢。

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. 在linux上你可以使用pipe功能:打开两个新管道,每个方向一个,然后使用fork创建子进程,之后,你通常关闭不使用的文件描述符(读取父节点上的结尾,写入管道的子节点)为父母发送给孩子,反之亦然为另一个管道)然后使用execve或其一个前端启动你的应用程序。

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). 如果您DUP2管道文件描述符到标准控制台文件句柄( STDIN_FILENO / STDOUT_FILENO ;每个进程分开),你甚至应该能够使用std::cin / std::cout与其他进程通信(您可能希望这样做只适用于孩子,因为您可能希望将控制台保留在父级中)。 I have no tested this, though, so that's left to you. 但是,我没有对此进行过测试,所以这就留给你了。

When done, you'd yet wait or waitpid for your child process to terminate. 完成后,你却waitwaitpid为您的孩子进程终止。 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM