title: Pipe: Bad file descriptors
body: <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;sys/wait.h&gt;
  #include &lt;string.h&gt;
  #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 &lt; 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 &lt; 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 &lt; number_process; i++){
              if (write(pipes[i][1], &amp;(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;
  </code></pre>
  
  <p>}</p>
  
  <p>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(). </p>
  
  <p>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? </p>