c - 使用 fork 的 C 程序中的错误文件描述符

标签 c fork dup2

这个程序应该模拟 posix shell 的管道命令。我试图模拟并想要工作的例子是“ls | nl”,但它没有,我不明白为什么。我已经调试这段代码好几个小时了,但没有成功。

我收到错误:“nl:输入错误:错误的文件描述符”,当我尝试不关闭任何文件描述符或仅关闭某些文件描述符(或仅在一个 fork 中,或仅在父级中,等等...),并且错误发生变化,或者它可以工作,但 nl 继续等待输入。无论如何,我很确定错误出现在 fork_cmdfork_cmds 中,并且与 close 有关。

我已经包含了所有代码。我知道 parser.h 没有任何问题。我知道这是相当糟糕的代码,但我认为它应该仍然有效。

我可能是盲人,但如果有人能帮助我弄清楚,我将非常感激。希望我和其他人可以从中学到一些东西。

#include "parser.h"

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

#define READ  0
#define WRITE 1


void fork_error() {
    perror("fork() failed)");
    exit(EXIT_FAILURE);
}

void close_error() {
    perror("Couldn't close file descriptor");
    exit(EXIT_FAILURE);
}


void fork_cmd(char* argv[], int n, int read_pipe[2], int write_pipe[2], int (*all_fds)[2]) {
    pid_t pid;

    switch (pid = fork()) {
    case -1:
        fork_error();
    case 0:
        if (read_pipe != NULL) {
            if (dup2(read_pipe[READ], STDIN_FILENO) < 0) {
                perror("Failed to redirect STDIN to pipe");
                exit(EXIT_FAILURE);
            }
        }

        if (write_pipe != NULL) {
            if (dup2(write_pipe[WRITE], STDOUT_FILENO) < 0) {
                perror("Failed to redirect STDOUT to pipe");
                exit(EXIT_FAILURE);
            }
        }

        for (int i = 0; i < n - 1; i++) {
            if (close(all_fds[i][READ]) == -1 || close(all_fds[i][WRITE] == -1)) {
                close_error();
            }
        }

        execvp(argv[0], argv);
        perror("execvp");
        exit(EXIT_FAILURE);

    default:
        printf("Pid of %s: %d\n", argv[0], pid);
        break;
    }
}

void fork_cmds(char* argvs[MAX_COMMANDS][MAX_ARGV], int n, int (*fds)[2]) {
    for (int i = 0; i < n; i++) {
        if (n == 1) {
            fork_cmd(argvs[i], n, NULL, NULL, fds);
        }
        // n > 1
        else if (i == 0) {
            fork_cmd(argvs[i], n, NULL, fds[i], fds);
        }
        else if (i == n - 1) {
            fork_cmd(argvs[i], n, fds[i - 1], NULL, fds);
        }
        else {
            fork_cmd(argvs[i], n, fds[i - 1], fds[i], fds);
        }
    }

    for (int i = 0; i < n - 1; i++) {
        if (close(fds[i][READ]) == -1 || close(fds[i][WRITE] == -1)) {
            close_error();
        }
    }
}

void get_line(char* buffer, size_t size) {
    getline(&buffer, &size, stdin);
    buffer[strlen(buffer)-1] = '\0';
}

void wait_for_all_cmds(int n) {
    // Not implemented yet!

    for (int i = 0; i < n; i++) {
        int status;
        int pid;
        if ((pid = wait(&status)) == -1) {
            printf("Wait error");
        } else {
            printf("PARENT <%ld>: Child with PID = %ld and exit status = %d terminated.\n",
                   (long) getpid(), (long) pid, WEXITSTATUS(status));
        }
    }
}

int main() {
    int n;
    char* argvs[MAX_COMMANDS][MAX_ARGV];
    size_t size = 128;
    char line[size];

    printf(" >> ");

    get_line(line, size);

    n = parse(line, argvs);

    // Debug printouts.
    printf("%d commands parsed.\n", n);
    print_argvs(argvs);

    int (*fds)[2] = malloc(sizeof(int) * 2 * (n - 1)); // should be pointer to arrays of size 2

    for (int i = 0; i < n - 1; i++) {
        if (pipe(fds[i]) == -1) {
            perror("Creating pipe error"); // Creating pipe error: ...
            exit(EXIT_FAILURE);
        }
        printf("pipe %d: read: %d, write: %d\n", i, fds[i][READ], fds[i][WRITE]);
    }

    fork_cmds(argvs, n, fds);
    wait_for_all_cmds(n);

    exit(EXIT_SUCCESS);
}

最佳答案

问题是 fork_cmdfork_cmds 中的括号之一位于错误的位置,当然应该是这样的: close(fds [i][写])。这是原始代码:

for (int i = 0; i < n - 1; i++) {
    if (close(fds[i][READ]) == -1 || close(fds[i][WRITE] == -1))<--
    {
            close_error();
    }
}

关于c - 使用 fork 的 C 程序中的错误文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48874302/

相关文章:

C++、linux、fork、execvp、waitpid 和 SIGTSP

C - 多个 fork 子项的命名管道

c - fork /管道/dup2 问题 : child process never exits

c++ - opengl 窗口不显示?

c++ - 拥有一个附带大量 DLL 的应用程序是不好的做法吗?

c - 将项目从 Visual Studio 2008 转换到 2010 时出错

c - 为什么这个带 fork 的程序会打印两次?

c - 为什么只有在重定向之前调用了 printf 才能使用 dup2 重定向到标准输出

c - 当你在一个用 dup2() 复制的管道文件描述符上调用 close() 时会发生什么?

c - 在 f# 中编写方法