1

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

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

我无法弄清楚问题出在哪里。这段代码的大部分与这个问题没有任何关系,但我把它包括在内,以防我在某个地方犯了一个草率的错误。该问题似乎出现在 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;

}

4

1 回答 1

1

你似乎没有initialize/add file descriptors in fd1[] & fd2[]。您正在尝试将empty/uninitializedint[] 存储在fd_set结构中。这就是为什么你得到EXC_BAD_ACCESS


笔记:

EXC_BAD_ACCESS 是什么意思?

EXC_BAD_ACCESS 意味着消息被发送到内存中没有类的实例来执行它的点。因此“访问不正确”</p>

当 EXC_BAD_ACCESS 发生时?

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

  1. 对象未初始化
  2. 一个对象已经被释放
  3. 其他不太可能发生的事情
于 2013-10-12T17:29:55.733 回答