将 stdout 提供给将执行 execv() 排序的子进程

Feeding stdout to a child process which will execv() sort

我正在尝试了解如何将一个进程的输出发送到子进程。我经历了一段学习文件描述符和管道的旅程。我想我快到了,但缺少一个关键组件。

这是我目前拥有的:

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

int main(int argc, char *argv[]) {
    int fd[2];
    pid_t sort_pid;


    /* Create the pipe */
    if(pipe(fd) == -1) {
        fprintf(stderr, "Pipe failed\n");
        exit(EXIT_FAILURE);
    }
    /* create child process that will sort */
    sort_pid = fork();
    if(sort_pid < 0) { // failed to fork
        fprintf(stderr, "Child Fork failed\n");
        exit(EXIT_FAILURE);
    }
    else if(sort_pid == 0) { // child process
        close(0);   // close stdin
        dup2(fd[0], 0); // make stdin same as fd[0]
        close(fd[1]); // don't need this end of the pipe
        execlp("D:/Cygwin/bin/sort", "sort", NULL);
    }
    else { // parent process
        close(1); // close stdout
        dup2(fd[1], 1); // make stdout same as fd[1]
        close(fd[0]); // don't need this end of the pipe
        printf("Hello\n");
        printf("Bye\n");
        printf("Hi\n");
        printf("G'day\n");
        printf("It Works!\n");
        wait(NULL);
    }

    return EXIT_SUCCESS;
}

这行不通,因为它似乎进入了无限循环之类的。我尝试了 wait() 的组合,但这也无济于事。

我这样做是为了学习如何在我的实际程序中应用这个想法。在我的实际程序中,我读取文件,逐行解析它们并将处理后的数据保存到结构的静态数组中。然后我希望能够根据这些结果生成输出,并使用 fork() 和 execv() 系统调用对输出进行排序。

这最终是为了uni的一个项目。

这些是我剖析的类似示例,以达到我目前所处的阶段:

此外,我阅读了相关系统调用的手册页以尝试理解它们。我承认我对管道的了解和使用它们基本上还是一无是处,因为这是我第一次尝试使用它们。

感谢任何帮助,甚至我可以自己查看更多信息来源。我似乎已经用尽了 google 搜索给我的大部分有用的东西。

sort 将读取直到遇到文件结尾。因此,如果您希望它完成,您必须关闭管道的写端。因为dup2,你有两份open file description,所以你需要

  1. close(fd[1]); 在调用 dup2
  2. 之后的任何时间
  3. close(1); 写完(新的)stdout

确保在第二个之前 fflush(stdout) 以确保您的所有数据确实进入了管道。

(这是一个简单的死锁示例:sort 正在等待管道关闭,这将在父级退出时发生。但是父级在完成等待子级之前不会退出退出...)