繁体   English   中英

Unix C - 将stdout重定向到管道然后再返回到stdout

[英]Unix C - Redirecting stdout to pipe and then back to stdout

我不确定是否可以执行以下操作,因为我无法通过Google找到任何问题/结果。 我想将fork()的stdout更改为管道,然后将其更改回正常的stdout。

这就是我所拥有的:

FirstExecutable:

int main()
{
      int fd[2]; //Used for pipe
      int processID;

      if(pipe(fd) == -1)
      {
            printf("Error - Pipe error.\n");
            exit(EXIT_FAILURE);
      }

      if((processID = fork()) == -1)
      {
            fprintf(stderr, "fork failure");
            exit(EXIT_FAILURE);
      }

      if(processID == 0)
      {
           int newFD = dup(STDOUT_FILENO);

          char newFileDescriptor[2];

          sprintf(newFileDescriptor, "%d", newFD);

          dup2 (fd[1], STDOUT_FILENO);

          close(fd[0]);

          execl("./helloworld", "helloworld", newFileDescriptor, NULL);
      }
      else
      { 
          close(fd[1]);

          char c[10];

          int r = read(fd[0],c, sizeof(char) * 10);

          if(r > 0)
               printf("PIPE INPUT = %s", c);
      }
}

你好,世界

int main(int argc, char **argv)
{
      int oldFD = atoi(argv[1]);

      printf("hello\n"); //This should go to pipe

      dup2(oldFD, STDOUT_FILENO);

      printf("world\n"); //This should go to stdout
}

期望的输出:

world
PIPE OUTPUT = hello

实际产量:

hello
world

尝试改变

  printf("hello\n");

  printf("hello\n");
  fflush(stdout);

这里的问题是缓冲。 出于效率原因,FILE句柄在写入时并不总是立即产生输出。 相反,他们在内部缓冲区中累积文本。

有三种缓冲模式,无缓冲,线缓冲和块缓冲。 无缓冲的句柄总是立即写入(stderr是无缓冲的)。 行缓冲句柄等到缓冲区已满或打印换行符( '\\n' )(如果stdout引用终端,则stdout为行缓冲)。 块缓冲句柄一直等到缓冲区已满(如果stdout没有引用终端,则stdout是块缓冲的)。

当你的helloworld程序启动时,stdout会进入管道,而不是终端,所以它被设置为块缓冲。 因此,printf调用只是将文本存储在内存中。 由于缓冲区未满,因此仅在stdout关闭时才刷新,在这种情况下会在程序退出时发生。

但是当程序退出时,文件描述符1(stdout)已经恢复,以引用父项的原始标准输出,而不是管道。 因此,缓冲的输出最终被写入原始标准输出。

fflush强制立即写入缓冲的文本。

暂无
暂无

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

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