Execl返回错误地址

Ado*_*dis -1 c debugging gcc posix exec

我真的很喜欢这个调试帮助.我从早上起凌晨4点就开始研究这个问题了.(我想在7个小时[上午11点]提供这个)

main.c中的所有东西都可以工作,但是当我创建一些子进程来运行带有execl的compute.c的编译文件时,它不会这样做并发送错误"Bad Address".

我已经附加了3个带有main.c和compute.c的pastebin链接以及一个包含我在下面提到的表的txt文件.

该程序假设从一个名为pinakes.txt的文件中读取带有整数的2个表,然后使用POSIX的共享内存API将这些表放在共享内存中并创建进程以计算它们的"行*列"总和并放置该总和在另一张桌子里.

sum += A[row][i] * B[i][column] = C[row][column]
Run Code Online (Sandbox Code Playgroud)

直到main.c下面的行应该正常工作(我调试了很多次).

ppid = getpid();
Run Code Online (Sandbox Code Playgroud)

编译然后运行

./main pinakes.txt
Run Code Online (Sandbox Code Playgroud)

main.c中

188行代码

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <errno.h>

int pinA_X = 0, pinA_Y = 0, pinB_X=0, pinB_Y=0;
int pinA[10][10], pinB[10][10], pinC[10][10];

main(int argc, char *argv[]){
    int pid, ppid;
    FILE *stream;
    // general variables
    int i, c, j, rc, converted, lines = 0;
    //flags
    int flagV=0, flagW=0, flagX=0, flagY=0, flagZ=0;

    //shared memory
    int dumpedArray[101];
    int size = sizeof(dumpedArray);
    int sid1 = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
    int sid2 = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
    int sid3 = shmget(IPC_PRIVATE, size, SHM_R | SHM_W);
    int* shared_A = (int*) shmat(sid1, NULL, 0);
    int* shared_B = (int*) shmat(sid2, NULL, 0);
    int* shared_C = (int*) shmat(sid3, NULL, 0);

    if(argc!=2){
        printf("wrong number of arguments\n");
        return -1;
    }else{
        stream = fopen(argv[1] , "r");
                while((c = getc(stream))!= EOF){
            if(flagZ == 0){
                if(flagX == 1){pinA_X = c - 48;flagX = 0;}
                if(c == 88){flagX = 1;}
                if(flagY == 1){pinA_Y = c - 48;flagY = 0;}
                if(c == 89){flagY = 1;}
                if(c == 90){flagZ = 1;}
            }else if(flagZ == 1){
                if(flagX == 1){pinB_X = c - 48;flagX = 0;}
                if(c == 88){flagX = 1;}
                if(flagY == 1){pinB_Y = c - 48;flagY = 0;}
                if(c == 89){flagY = 1;}
            }
        }

        fclose(stream);

        printf("pinA[%d][%d] * pinB[%d][%d] = C[%d][%d]\n\n", pinA_X, pinA_Y, pinB_X, pinB_Y, pinA_X, pinB_Y);

        // get A
        stream = fopen(argv[1] , "r");
        i=0;j=0;
        while((c = getc(stream))!= EOF){
            if(i <= pinA_X && j <= pinA_Y){
                if(flagW == 0){
                    if(c == 87){
                        flagW = 1;
                    }
                }else{
                    if(c > 47 && c < 58){
                        pinA[i][j] = c - 48;
                        j++;
                    }
                    if(c == 13){
                        j=0;
                        i++;
                    }
                }
            }
        }
        fclose(stream);

        // get B
        stream = fopen(argv[1] , "r");
        i=0;j=0;
        while((c = getc(stream))!= EOF){
            if(i <= pinB_X && j <= pinB_Y){
                if(flagV == 0){
                    if(c == 86){
                        flagV = 1;
                    }
                }else{
                    if(c > 47 && c < 58){
                        pinB[i][j] = c - 48;
                        j++;
                    }
                    if(c == 13){
                        j=0;
                        i++;
                    }
                }
            }
        }
        fclose(stream);

        // print A

        printf("A={\n");
        for(j=0; j<pinA_X;j++){
            for(i=0;i<pinA_Y;i++){
                printf(" %d", pinA[j][i]);
            }
            printf("\n");
        }
        printf("}\n\n");

        // print B

        printf("B={\n");
        for(j=0; j<pinB_X;j++){
            for(i=0;i<pinB_Y;i++){
                printf(" %d", pinB[j][i]);
            }
            printf("\n");
        }
        printf("}\n");

        // Save pinA to shared Memory
        converted = 0;

        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                converted = (i * 10) + j;
                shared_A[converted] = pinA[i][j];
            }
        }


        // Save pinA to shared Memory
        converted = 0;

        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                converted = (i * 10) + j;
                shared_B[converted] = pinB[i][j];
            }
        }

        // Push size of arrays in shared memory
        shared_A[100] = pinA_X;
        shared_A[101] = pinA_Y;
        shared_B[100] = pinB_X;
        shared_B[101] = pinB_Y;

        ppid = getpid();

        for(i=0; i<pinA_X; i++){
            for(j=0; j<pinB_Y; j++){
                if(ppid == getpid()){
                    pid = fork();
                    if(pid==0){
                        if(execl("./compute", "compute", i, j, sid1, sid2, sid3, NULL) == -1){
                            printf("error exec\n");
                            printf("Error opening file: %s\n", strerror(errno));
                        };
                    }else if(pid<0){
                        printf("\nDen egine h fork!\n");
                    }else{
                        wait(0);
                    }
                }
            }
        }

        //print C
        converted = 0;

        printf("C={\n");
        for(i=0;i<10;i++){
            for(j=0;j<10;j++){
                converted = (i * 10) + j;
                pinC[i][j] = shared_C[converted];
                printf(" %d", pinC[i][j]);
            }
            printf("\n");
        }
        printf("}\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

回答这个问题既不直接compute.c也不pintakes.txt直接相关.

Jon*_*ler 6

出现错误的地址问题是因为您运行:

for(i=0; i<pinA_X; i++){
    for(j=0; j<pinB_Y; j++){
        if(ppid == getpid()){
            pid = fork();
            if(pid==0){
                if(execl("./compute", "compute", i, j, sid1, sid2, sid3, NULL) == -1){
Run Code Online (Sandbox Code Playgroud)

execl()必须是字符串的参数; i并且j显然不是字符串(并且sid1,sid2并且sid3是三个共享内存块的标识符).

将这些值转换为字符串,然后重试.

您的程序使用IPC_PRIVATE创建共享内存,因此您的代码compute.c(通过执行execl()将难以实现.您可能会转移这样的共享内存ID;我不确定.

我想我会使用一个共享内存段.

看起来你的阅读代码也会将相同的数据读入两个数组 - 但我可能误读了它.

最后,您的PasteBin示例将在23小时后到期.这限制了你的问题的有用性.您应该将数据转移到问题中 - 我建议,没有选项卡和tabstops设置为4而不是8.(或者使用更多函数来防止这种深度缩进.)


Dan*_*her 5

您将ints传递给execl,那些应该都是以 0 结尾的字符串。此外,finalNULL必须强制转换为Char*.