Pipe: Bad file descriptors

南楼画角 提交于 2019-12-24 00:44:02

问题


I know this kind of posts have been asked previously, but their level are clearly higher than mind, I still don't get it after reading their post, so I decide to post this question again from here.

I am learning multi-processes communication using pipe, I have confronted to this error called Bad file descriptors, I don't understand why I am having this error in my code.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#define SIZE 50

struct record {
    int freq;
    char word[SIZE];
};

int main(){

int number_process = 3;
int pipes[number_process][2];

struct record r1;
r1.freq = 10;
strcpy(r1.word, "Cat");

struct record r2;
r2.freq = 20;
strcpy(r2.word, "Elephant");

struct record r3;
r3.freq = 30;
strcpy(r3.word, "Dragon");

struct record records_array[3] = {r1, r2, r3}; 

for (int i = 0; i < number_process; i++){

    if (pipe(pipes[i]) == -1){
        perror("pipe");
        exit(1);
    }

    // Create children.
    pid_t fork_result = fork();
    if (fork_result == -1){
        perror("Parent fork");
        exit(1);
    } else if (fork_result == 0){

        if (close(pipes[i][0]) == -1){
            perror("Child closes reading port");
            exit(1);
        }

        // Later children is going to close all reading port from pipe that parent creates.
        for (int child_no = 0; child_no < i; child_no++) {
            if (close(pipes[child_no][0]) == -1) {
                perror("close reading ends of previously forked children");
                exit(1);
            }
        }

        // Now, I am trying to write each strct record member from the above array into the pipe
        // when I run the program, it won't allow me to do so because of bad file descriptor exception.
        for (int j = 0; j < number_process; i++){
            if (write(pipes[i][1], &(records_array[j]), sizeof(struct record)) == -1){
                perror("write from child to pipe");
                exit(1);
            }
        }

        // Finishing writing, close the writing end in pipe.
        if (close(pipes[i][1]) == -1){
            perror("Child closes writing port");
            exit(1);
        }
        // Terminate the process.
        exit(0);

    } else {
        // Parent is closing all the writing ends in pipe.
        if (close(pipes[i][1]) == -1){
            perror("Parent close writing");
            exit(1);
        }

    }

}
    return 0;

}

When I finish compiling and run the executable, it just tells me bad file descriptors occurs. I tried to use gdb to take a closer look at where this error might occur, and I notice that gdb reports this error even before I call write().

I feel completely lost in this write and pipe concept, can someone kindly please explain to me what I did wrong somewhere in the process?


回答1:


Your issue has nothing to do with any of the system calls you're using. It is more mundane. for (int j = 0; j < number_process; i++) is a bug. You are using i to access your array of file descriptors and incrementing it incorrectly. You meant to increment j.



来源:https://stackoverflow.com/questions/55219339/pipe-bad-file-descriptors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!