使用c ++创建rw管道的最佳方法是什么?

时间:2010-08-12 13:45:58

标签: c++ linux pipe

我有一个程序A需要将命令发送到程序B的stdin并读回该程序B的输出。(用C ++编程,而不是linux)

ProgramA - >发信A - > ProgramB ProgramA< - B< - ProgramB

的输出

我实际上有第一部分,向B发送命令,使用popen()。我知道popen只是一种方式。

那么,使用c ++进行双向处理的最佳方法是什么?

3 个答案:

答案 0 :(得分:4)

使用posix功能(因此,适用于linux和任何符合posix标准的系统),您可以使用pipe / execl / dup的组合。简而言之,会发生什么:

  • 创建2个管道(一个用于读取,一个用于写入子级)
  • 分叉当前进程。这保持打开相同的fd
  • 关闭当前的stdin / stdout。然后使用dup(使用最低可用描述符来复制你给它的内容)
  • 执行子进程

注意需要一些冲洗。 父母的代码是:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main ()
{
    pid_t pid;
    int pipe_in[2];   /* This is the pipe with wich we write to the child process. */
    int pipe_out[2];  /* This is the pipe with wich we read from the child process. */

    if (pipe (pipe_in) || pipe (pipe_out)) {
        fprintf (stderr, "Error in creating pipes!\n");
        exit (1);
    }

    /* Attempt to fork and check for errors */
    if ((pid = fork ()) == -1) {
        fprintf (stderr, "Error in fork!\n");
        exit (1);
    }

    if (pid) {
        /* The parent has the non-zero PID. */
        char temp[100];
        int result;
        FILE* child_in;
        FILE* child_out;

        child_in = fdopen(pipe_out[0],"r");
        child_out = fdopen(pipe_in[1],"w");
        close(pipe_out[1]);
        close(pipe_in[0]);

        fprintf(child_out, "something\n"); 

        fgets(temp,100,child_in);
        printf(" Read from child %s \n", temp); 

        /* Send a command to the child. */
        fprintf(child_out, "quit\n"); 
        fflush(child_out);

        fgets(temp,100,child_in);
        printf(" Read from child %s \n", temp);

        wait (&result);             /* Wait for child to finish */
    }
    else {
        /* The child has the zero pid returned by fork*/
        close (1);
        dup (pipe_out[1]);  /* dup uses the lowest numbered unused file descriptor as new descriptor. In our case this now is 1. */

        close (0);          /* dup uses the lowest numbered unused file descriptor as new descriptor. In our case this now is 0. */
        dup (pipe_in[0]);  

        close (pipe_out[0]);
        close (pipe_out[1]);
        close (pipe_in[0]);
        close (pipe_in[1]);
        execl ("child", "child", NULL);
        exit(1); /* Only reached if execl() failed */
    }
    return 0;
}

一个简单的孩子是:

#include <stdio.h>
#include <string.h>

int main ()
{
    char temp[100];

    do {
        printf ("In child: \n");
        fflush (stdout);
        fgets (temp, 100, stdin);
        printf ("Child read %s\n", temp);
        fflush (stdout);
    } while (!strstr (temp, "quit"));

    return 0;
}

您可以使用以下命令编译它们:

gcc -o parent parent.c
gcc -o child child.c
./parent

你会看到

从孩子读书在孩子:  阅读儿童阅读戒烟

答案 1 :(得分:1)

您可以尝试来自Glib的g_spawn_async_with_pipes

此函数运行一个进程并返回其输入/输出文件描述符。

我在我的C ++程序中使用它:

gchar** cmd_argv;
g_shell_parse_argv("your command", &cmd_argc, &cmd_argv, NULL);
g_spawn_async_with_pipes
 (NULL, //Working directory, Inherited from parent (our program)
  cmd_argv,
  NULL, //Null to inherit environment variables from parent
  G_SPAWN_SEARCH_PATH, //Flags
  NULL,
  NULL,
  NULL, //Returns the process ID of child (which runs from our program)
  &stdin_fd, //Standard input (file descriptor)
  &stdout_fd, //Standard output (file descriptor)
  &stderr_fd, //Standard error (file descriptor)
  NULL); //GError*

...

//we have to free cmd_argv
for (int i = 0; i < cmd_argc; ++i) g_free (cmd_argv [i]);

stdin_fdstdout_fdstderr_fd是整数(作为文件描述符):

gchar* out;
GIOChannel* io = g_io_channel_unix_new(stdout_fd); //g_io_channel_win32_new_fd
while(g_io_channel_read_line(io, &out, NULL, NULL, NULL) == G_IO_STATUS_NORMAL)
{
    //...
    g_free(out);
}

然而,它不是C ++库,但可能很有用(当你想用C ++编写GTK应用程序时)。

答案 2 :(得分:0)

请参阅here,了解Jeremy Siek(他在许多提升库中工作)的一个非常密切相关的问题。那时(2002年),在ACE库中可以找到唯一能够回答你问题的答案。有关详细信息,请参阅here

不确定这是您问题的正确答案,但它可能有所帮助。