Q:

Segmentation fault when using select() to monitor pipes

I am working on a project the premise of which is to create a process tree where a parent process sends half of a string(of digits) to each of its two child processes through pipes, then, when the number is <=2 tally the number of times the digit occurs and pass the tally (hopefully in the form of an array of ints, back up to the parent.
I'm still in the initial stages of building this thing and I keep getting caught up using select(); Right now I am getting a SEG_FAULT when trying to execute the program, and when I threw it into Eclipse, the error (listed as EXC_BAD_ACCESS: could not access memory) seems to occur the second time I try to set a file descriptor to the read fd set.
I can't figure out where the problem is. Most of this code doesn't have anything to do with the issue, but I included it in case I made a sloppy mistake somewhere. The issue seems to occur in the bit_count function. I labeled it.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/wait.h>

int bit_count(char *passed, int len);

main(int argc, char *argv[]){
FILE *fp;
fp = fopen(argv[1], "r");   //open the file for reading
if( fp == NULL ) {          //file open error
        perror ("Error opening file");
        exit(EXIT_FAILURE);
}

fseek(fp, 0, SEEK_END);     //seek to find file size
long int length = ftell(fp);    //ftell file length
rewind(fp);             //rewind pointer to begin read
char *string = malloc(length+1);    //malloc space for the string from the file.
fread(string, 1, length, fp);       //begin read
fclose(fp);

if( length < 2){    //make sure string length is greater than 2. Just in case.
    printf("File too small. Must have greater than 2 binary digits\n");
    return 0;
}

bit_count(string, length);          //call bit_count
return 0;
}

int bit_count(char *passed, int len){

int fd1[2], fd2[2];       //file descriptors used for left and right children
fd_set read_set;
fd_set write_set;
struct timeval tv;
tv.tv_sec = 60;
tv.tv_usec = 0;
FD_ZERO(&read_set);
FD_SET(fd1[0], &read_set);
FD_SET(fd2[0], &read_set);    //WHERE EXC_BAD_ACCESS OCCURS
pipe(fd1);
pipe(fd2);

pid_t kid = fork();
if(kid == -1) printf("forking failed.");
if (kid == 0){  //first child process
    int retval = select(2, &read_set, NULL, NULL, &tv);
    if (retval == -1){
        printf("Select Error.\n");
    }
    char *lstring = malloc(len/2+1);
    read(fd1[0], lstring, sizeof(lstring));
    printf("left %s", lstring);
    exit(1);
    }
else{       //parent process
    pid_t kid2 = fork();
    if (kid2 == 0) {    //second child process

                  int retval = select(2, &read_set, NULL, NULL, &tv);
            if (retval == -1){
                printf("Select Error.\n");
            }
            char *rstring = malloc(len/2);
            read(fd2[0], rstring, sizeof(rstring));
            printf("Right %s", rstring);
            //execl("child.c", parent);
            exit(1);
    }

    else{

        int status;

        //create character arrays for first and second half of string
                    //then copy
        char *lstring = malloc(len/2+1);
        char *rstring = malloc((len/2)+2);
        strncpy(lstring, passed, len/2);
        strcpy(rstring, &passed[(len/2)]);
        printf("ppp %s", lstring);

        write(fd1[1], lstring, sizeof(lstring));
        write(fd2[1], rstring, sizeof(rstring));
        waitpid(kid, &status, NULL);
        waitpid(kid2, &status, NULL);

        }

    return 0;
}
return 0;

}

A:

You don't seem to initialize/add file descriptors in fd1[] & fd2[]. You are trying to store empty/uninitialized int[] in fd_set structure. That's why you are getting EXC_BAD_ACCESS 

NOTE:
What does EXC_BAD_ACCESS mean?
EXC_BAD_ACCESS means that message was sent to a point in the memory where there’s no instance of a class to execute it. Thus “bad access”
When EXC_BAD_ACCESS happen?
You will get EXC_BAD_ACCESS in 3 cases:

An object is not initialized
An object is already released
Something else that is not very likely to happen

