使用select()监视管道时出现分段错误

时间:2013-10-12 17:09:15

标签: c pipe posix-select

我正在开发一个项目,其前提是创建一个进程树,其中父进程通过管道向其两个子进程中的每一个发送一半字符串(数字),然后,当该数字为< = 2计算数字出现的次数并通过计数(希望以一组整数的形式,然后返回到父级。

我仍处于构建此事的初始阶段,并且我一直在使用select();现在我在尝试执行程序时得到一个SEG_FAULT,当我把它扔进Eclipse时,错误(列为EXC_BAD_ACCESS:无法访问内存)似乎是第二次我尝试将文件描述符设置为读取时fd set。

我无法弄清楚问题出在哪里。这段代码中的大部分都没有与这个问题有任何关系,但我把它包含在内,以防我在某处犯了一个草率的错误。问题似乎发生在bit_count函数中。我贴了标签。

#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;

}

1 个答案:

答案 0 :(得分:1)

您似乎不是initialize/add file descriptors in fd1[] & fd2[]。您正试图在empty/uninitialized结构中存储fd_set int []。这就是你得到EXC_BAD_ACCESS

的原因

注意:

EXC_BAD_ACCESS是什么意思?

EXC_BAD_ACCESS表示消息被发送到内存中没有类的实例来执行它的点。因此“糟糕的访问”

EXC_BAD_ACCESS何时发生?

您将在3种情况下获得EXC_BAD_ACCESS:

  1. 未初始化对象
  2. 对象已经发布
  3. 其他不太可能发生的事情