管道:关闭管道数组中的文件描述符

Pipe: Closing file descriptors in an array of pipe

我正在学习 Linux 并且正在为我的系统编程课程学习管道这类东西,我现在很难理解管道数组中的关闭文件描述符。

 // write the code to loop over the command line arguments (remember to skip the executable name)
for (int i = 1; i < argc; i++) {
    // call pipe before we fork
    if ((pipe(pipe_fd[i-1])) == -1) {
        perror("pipe");
        exit(1);
    }
    // call fork
    int result = fork();
    if (result < 0) {   // case: a system call error
        // handle the error
        perror("fork");
        exit(1);
    } else if (result == 0) {  // case: a child process
        // child does their work here
        // child only writes to the pipe so close reading end
        if (close(pipe_fd[i-1][0]) == -1) {
            perror("close reading end from inside child");
            exit(1);
        }
        // before we forked the parent had open the reading ends to
        // all previously forked children -- so close those
        int child_no;
        for (child_no = 0; child_no < i-1; child_no++) {
            if (close(pipe_fd[child_no][0]) == -1) {
                perror("close reading ends of previously forked children");
                exit(1);
            }
        }
        int len = strlen(argv[i]);
        // write len to the pipe as an integer
        if (write(pipe_fd[i-1][1], &len, sizeof(int)) != sizeof(int)) {
            perror("write from child to pipe");
            exit(1);
        }
        // I'm done with the pipe so close it
        if (close(pipe_fd[i-1][1]) == -1) {
            perror("close pipe after writing");
            exit(1);
        }
        // exit so I don't fork my own children on next loop iteration
        exit(0);
    } else {
        // in the parent but before doing the next loop iteration
        // close the end of the pipe that I don't want open
        if (close(pipe_fd[i-1][1]) == -1) {
            perror("close writing end of pipe in parent");
            exit(1);
        }
    }
}

我将列出我目前所了解的内容:

  1. 我明白 parent 和 child 进程需要关闭那些他们不需要使用的 fds,在这种情况下 child 正在写入 parent,所以parent需要关闭写端口,child需要关闭读端口。
  2. 我了解文件描述符在 parent 进程和 children 进程之间共享。

上面的代码是我的lecture slide给出的,具体有一点让我感到困惑。

在循环中,我观察到每个 child 都会在 fork 创建此 child 后关闭其读取端口,执行此操作的代码是:

else if (result == 0) {  // case: a child process
    // child does their work here
    // child only writes to the pipe so close reading end
    if (close(pipe_fd[i-1][0]) == -1) {
        perror("close reading end from inside child");
        exit(1);
    }

目前我的理解是,每个child在被fork分娩后都会关闭自己的阅读端口,我认为后者children创建的SHOULD NOT担心关闭之前 children 的阅读端口。

但是看了这段代码,我的理解似乎不正确:

        // before we forked the parent had open the reading ends to
    // all previously forked children -- so close those
    int child_no;
    for (child_no = 0; child_no < i-1; child_no++) {
        if (close(pipe_fd[child_no][0]) == -1) {
            perror("close reading ends of previously forked children");
            exit(1);
        }
    }

我不明白为什么后面的children要关闭前面children的阅读端口,那些children的阅读端口不是已经关闭了吗已创建?

感谢您帮助我。 :)

一个描述符在打开它的所有进程都关闭它之前并没有真正关闭。由于每个 child 都继承了前一个进程的所有管道描述符,因此它们应该关闭所有未使用的管道描述符。

关闭读端口的主要原因是写进程在 reader 退出后尝试写入管道时会收到错误或信号。如果其他 children 保持所有读取端口打开,则在所有后续 children 退出之前不会发生这种情况。