C管道: Bad file descriptor

标签 c fork producer-consumer

我在 C 中有一个简单的生产者消费者程序,尝试用 fork 解决它 当生产者试图在管道上写入时,我得到了错误: 我用相同的逻辑编写了另一个程序,但这个程序没有给我任何线索,让我知道为什么?

生产者无法在管道上写入项目:错误的文件描述符

有人知道为什么我会收到此错误吗? 谢谢

#define READ 0
#define WRITE 1
int mutex = 1, full = 0, empty = BUFFER_SIZE, x = 0;

void consumer();

void producer();

int wait_(int);

int signal_(int);

int pipefd[2];

int main() {
    printf("Starting producer-consumer problem!\n");
    //We intend to run the producer in parent process and the consumer in the child process
    if (pipe(pipefd) == -1) {       /* An error has occurred. */
        fprintf(stderr, "%s", "The call to pipe() has failed.\n");
        exit(EXIT_FAILURE);
    }
    for (int j = 0; j < sizeof(pipefd); j++) {
        if (pipe(&pipefd[j]) < 0) { //Initialize each pipe appropriately
            perror("Error in making pipe...");
        }
    }
    pid_t pid = fork();
    if (pid < 0) {
        perror("**********Error in creating fork()!**************\n");
        exit(STDERR_FILENO);
    } else if (pid == 0) {
        consumer();//We intend to run the consumer in child
    } else {
        producer();//We intend to run the producer in parent
    }
    return 0;
}

int wait_(int s) {
    return (--s);
}

int signal_(int s) {
    return (++s);
}

void producer() {
    printf("Starting Producer\n");
    //while (1) {
    //sleep(1);
    if (close(pipefd[READ]) != 0) {
        perror("Error in closing reading pipe");
    }
    if (write(pipefd[WRITE], &full, 1) < 0) {
        perror("Producer failed to write item on pipe");
    }
    if ((mutex == 1) && (empty != 0)) {
        mutex = wait_(mutex);
        full = signal_(full);
        empty = wait_(empty);
        x++;
        printf("Producer produces the item %d\n", x);
        mutex = signal_(mutex);
    }
    if (close(pipefd[WRITE]) != 0) {
        perror("Error in closing writing pipe");
    }
    //}
}

void consumer() {
    printf("Starting Consumer\n");
    //while (1) {
    //sleep(1);
    int status = 0;
    wait(&status);              /* wait for all children to return back the result */
    if (close(pipefd[WRITE]) != 0) {
        perror("Error in closing reading pipe");
    }
    if (read(pipefd[READ], &full, 1) > 0) {
        printf("Consumer\t%d\n", full);
    }
    if ((mutex == 1) && (full != 0)) {
        mutex = wait_(mutex);
        full = wait_(full);
        empty = signal_(empty);
        printf("Consumer consumes item %d\n", x);
        x--;
        mutex = signal_(mutex);
    }
    if (close(pipefd[READ]) != 0) {
        perror("Error in closing reading pipe");
    }
    //}
}

最佳答案

sizeof 运算符返回大小以字节为单位。因此,在 int 为四个字节的典型系统上,sizeof(pipefd) 将产生值 8。这不是您循环的正确元素数量。

此外,pipe(&pipefd[j]) 也不正确。 pipefd 中的两个管道已经“适本地”初始化。不需要任何更多的初始化。特别是因为在这种情况和之前的情况下你都会有 undefined behavior .

关于C管道: Bad file descriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47490077/

相关文章:

c - 请解释一下这段C代码中的第四行

c - 没有输出到 ALSA 音序器的预定事件

c - 段错误 - 生产者消费者

c++ - 生产者-消费者日志文件输出重复

将嵌套循环转换为递归

子进程卡在管道内

c - 为什么输出后多了一个百分号?

c++ - ZeroMQ 与 fork 服务器中的所有子进程共享上下文

C# 在 Web 服务中实现生产者/消费者队列

c - "alignment trap"错误是什么意思?