【问题标题】:Redirect child process's stdin and stdout to pipes将子进程的标准输入和标准输出重定向到管道
【发布时间】:2013-07-16 23:37:19
【问题描述】:

编辑: 解决办法是

 int c1=dup2(pipes[0][1],STDOUT_FILENO);
 int c2=dup2(pipes[1][0],STDIN_FILENO);


 setvbuf(stdout,NULL,_IONBF,0);

设置标准输出为非缓冲是 SETVBUF。即使我在目标不是实际屏幕时打印换行符,我猜它也会被缓冲。



编辑: 当我将 fflush(stdout) 放在第 1 行之后,将 fflush(fout) 放在第 4 行之后,它按预期工作。但是,如果没有 LINE 1 之后的 fflush(stdout),它就无法工作。问题是我无法将 fflush 放入我计划的程序中运行。


我正在尝试从我的进程中启动另一个程序。我无权访问它的代码,但我知道它使用标准输入和标准输出进行用户交互。我试图通过创建 2 个管道、分叉并将孩子的标准输入/标准输出重定向到正确的管道末端来启动该程序。关键是父级应该能够通过文件描述符与子级通信,而它的标准输入/标准输出应该是完整的。 POPEN 系统调用只打开单向管道。以下代码几乎可以工作。

有 4 行标记为 LINE 1..4。

LINE 1 是子发送到管道, LINE 2 是从管道接收的孩子, 第 3 行是父发送到管道, 第 4 行是父级从管道接收,

这只是一个确保一切正常的玩具示例。问题是所有 4 行 LINE1..4 都未注释我在终端上看到的输出是

PARENT1: -1
FD: 1 0    4 5    0 1
DEBUG1: 0
DEBUG2: 0

如果只有第 1 行和第 3 行未注释,我会看到连续的数据流。如果只有第 2 行和第 4 行未注释,也会发生同样的情况。但是,我想要一个完整的双向通信。另外添加注释的 SLEEP 不会改变行为。

这可能是什么问题。我想知道为什么没有双向POPEN。

int pid;
int pipes[2][2];

pipe(pipes[0]);
pipe(pipes[1]);

pid=fork();

if(pid==0)
  {
  //usleep(1000000);
  close(pipes[0][0]);
  close(pipes[1][1]);

  int c1=dup2(pipes[0][1],STDOUT_FILENO);
  int c2=dup2(pipes[1][0],STDIN_FILENO);
  //int c2=dup2(STDIN_FILENO,pipes[1][0]);

  fprintf(stderr,"FD: %d %d    %d %d    %d %d\n",c1,c2,pipes[0][1],pipes[1][0],STDIN_FILENO,STDOUT_FILENO);

  //FILE*fout=fdopen(pipes[0][1],"w");
  //FILE*fin =fdopen(pipes[1][0],"r");
  while(1)
    {
    static int c1=0;
    fprintf(stderr,"DEBUG1: %d\n",c1);
    printf("%d\n",c1);                      // LINE 1
    fprintf(stderr,"DEBUG2: %d\n",c1);
    scanf("%d",&c1);                        // LINE 2
    fprintf(stderr,"DEBUG3: %d\n",c1);
    c1++;
    }
  //fclose(fout);
  //fclose(fin);
  return 0;
  }

close(pipes[0][1]);
close(pipes[1][0]);

char buffer[100];
FILE*fin=fdopen(pipes[0][0],"r");
FILE*fout=fdopen(pipes[1][1],"w");
while(1)
  {
  int c1=-1;
  printf("PARENT1: %d\n",c1);
  fscanf(fin,"%d",&c1);                         // LINE 3
  printf("Recv: %d\n",c1);

  fprintf(fout,"%d\n",c1+1);                    // LINE 4
  printf("PARENT3: %d\n",c1+1);
  }
fclose(fin);
fclose(fout);

【问题讨论】:

  • 回答自己的问题就好了!与其用您的解决方案编辑原始问题,不如将其作为答案发布,然后接受。

标签: c linux unix posix


【解决方案1】:

您的代码很长,所以我不确定我是否了解所有内容,但您为什么不使用 select ? 你想在一个 tird 进程中重定向子进程的输出还是在你的父进程中使用它?

下面的例子是子进程中的cat。

#include <unistd.h>
#include <stdlib.h>

int     main()
{
  pid_t pid;
  int   p[2];


  pipe(p);
  pid = fork();
  if (pid == 0)
    {
      dup2(p[1], 1); // redirect the output (STDOUT to the pipe)
      close(p[0]);
      execlp("cat", "cat", NULL);
      exit(EXIT_FAILURE);
    }
  else
    {
      close(p[1]);
      fd_set rfds;
      char      buffer[10] = {0};

       while (1)
        {
          FD_ZERO(&rfds);
          FD_SET(p[0], &rfds); 
          select(p[0] + 1, &rfds, NULL, NULL, NULL); //wait for changes on p[0]
          if(FD_ISSET(p[0], &rfds))
            {
              int       ret = 0;
              while ((ret = read(p[0], buffer, 10)) > 0) //read on the pipe
                {
                  write(1, buffer, ret); //display the result
                  memset(buffer, 0, 10);
                }
            }
        }
    }
}

【讨论】:

  • 其实我想出了解决办法。将 stdout 设置为非缓冲是 SETVBUF(即使我正在打印换行符,但目标不是实际的屏幕,我猜它会变成缓冲的。
  • 我这样做的原因是我正在启动的程序有一个 PRINT-EVAL 循环,我想以编程方式对其进行响应。
  • @user1068779 很高兴您找到了解决方案。您仍然可以在父进程中向 p[1] 写入响应