title: Why can't I write to or read from pipes?
body: <p>I've seen a lot of similar questions, but all of them addressed specific cases and did not assist me in finding a solution. I would appreciate any feedback on my situation, which is as follows:</p>
  
  <p>I am trying to write a program to get character counts from a text file. The program works by forking four mappers and 26 reducers, and creating pipes for each of them. The parent process separates the input into four lines and passes one to each mapper, which counts the number of each character in its line. Each mapper then passes the counts on to the appropriate reducers, which sum up all four counts and print the result.</p>
  
  <p>Below is my code thus far:</p>
  
  <pre><code> int main(int argc, char *argv[])
  {
    int i = 0;
  
    FILE *input = fopen("input.txt", "r");
  
    // Things for reading line-by-line: see getline reference on man7.org
    char *line = NULL;
    size_t len = 0;
  
    // Where we'll store the messages
    static char message[MSGSIZE];
  
    for(i = 0; i &lt; NUMREDUCERS; i++)
      {
        pipe(reducer_pipes[i]);
      }
  
    for(i = 0; i &lt; NUMMAPS; i++)
      {
        // Step 1: Create pipes for communication using the system call pipe()
        pipe(mapper_pipes[i]);
  
        // Step 2: Fork a number of mappers (4).
        if (fork() == 0)
      {
        // Don't want to close the write pipe yet
        // Child process: one of the mappers
  
        read(mapper_pipes[i][0], message, MSGSIZE); // Read from reading end
        char *msg = "Error reading from pipe";
        check_errors(msg);
  
        // Get char count and write to pipe
        int j = 0;
        int ccount = 0;
        for(j = 0; j &lt; NUMREDUCERS; j++)
          {
            // Count up the number of chars
            ccount = count_char(message, (char) (j + 97), MSGSIZE);
  
            // Write to the appropriate reducer pipe
            write(reducer_pipes[j][1], (char *) ccount, MSGSIZE);
            msg = "error writing to reducer pipe";
            check_errors(msg);
  
          }
  
        exit(EXIT_SUCCESS);
      }
        else
      {
        getline(&amp;line, &amp;len, input);
        // Parent process
  
        write(mapper_pipes[i][1], line, (int) len);
        char *msg = "Error writing to pipe";
        check_errors(msg);
      }
      }
  
    return 0;
  }
  </code></pre>
  
  <p>The problem I am encountering is that I cannot write to the reducer pipes. I get a bad address error whenever I attempt to write to, read from, or close them. Did they somehow expire? Did I not create them correctly?
  If anyone has input to offer, I would greatly appreciate it.</p>
  
  <p>Quick edit: I have removed all of my "close" statements, as they had the same issue. I have, however, tried closing the pipes where they should be closed, only to find the same error message.</p>