Execl 返回错误地址

发布于 2024-12-25 12:14:14 字数 6392 浏览 6 评论 0原文

我真的很喜欢这方面的一些调试帮助。我从早上到凌晨 4 点就一直在做这件事。 (我想在 7 小时内交付这个 [上午 11 点])

main.c 中的所有内容都可以工作,但是当我创建一些子进程来使用 execl 运行compute.c 的编译文件时,它不会执行此操作并发送“错误地址”错误。

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

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

sum += A[row][i] * B[i][column] = C[row][column]

main.c 中的以下行之前的所有内容都应该正常工作(我调试了多次)。

ppid = getpid();

编译然后运行

./main pinakes.txt

​​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");
    }
}

compute.cpintakes.txt 都与回答这个问题没有直接关系。

I would really love some debugging help in this one. I've been working on this since the morning and its 4am. (I'm suppose to deliver this in 7 hours [11am])

Everything in main.c works but when I create some child processes to run compute.c's compiled file with execl it doesnt do it and sends an error of "Bad Address".

I've attached 3 pastebin links with main.c and compute.c and a txt file containing the tables I mention below.

The program is suppose to read 2 tables with integers from a file called pinakes.txt and then by using POSIX's shared memory API to place those tables in shared memory and create processes to calculate a 'row * column' sum from them and place that sum in another table.

sum += A[row][i] * B[i][column] = C[row][column]

Everything until the line below from main.c should work properly (I debugged it numerous times).

ppid = getpid();

compile and then run

./main pinakes.txt

main.c

188 lines of code

#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");
    }
}

Neither compute.c nor pintakes.txt is directly relevant to answering this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2025-01-01 12:14:14

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

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){

The argument to execl() must be strings; ij 显然不是字符串(而 sid1sid2sid3 是三个共享内存块的标识符)。

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

您的程序使用 IPC_PRIVATE 创建共享内存,因此 compute.c 中的代码(通过 execl() 执行)将很难工作。您可能会得到不再像这样传输共享内存 ID;我

认为我会使用单个共享内存段,

但看起来您的读取代码会将相同的数据读入两个数组。 可能是误读了。

最后, 你的 PasteBin 示例将在 23 小时内过期。这限制了你的问题的有用性。我建议,不要将制表符和制表位设置为 4 而不是 8。(或者使用更多函数来防止这种情况。深压痕。)

The bad address problem arises because you run:

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){

The arguments to execl() must be strings; i and j are manifestly not strings (and sid1, sid2 and sid3 are the identifiers for three chunks of shared memory).

Convert those values to strings and try again.

Your program creates the shared memory with IPC_PRIVATE, so your code in compute.c (which is executed via execl() is going to be hard to make work. You may get away with transferring the shared memory IDs like that; I'm not sure.

I think I'd be using a single shared memory segment.

It also looked like your reading code is going to read the same data into the two arrays - but I may have been misreading it.

Finally, your PasteBin examples expire in 23 hours. That limits the usefulness of your question. You should really transfer the data into the question - with, I suggest, no tabs and tabstops set at 4 rather than 8. (Or use more functions to prevent such deep indentation.)

少年亿悲伤 2025-01-01 12:14:14

您将 int 传递给 execl,这些应该都是以 0 结尾的字符串。此外,最终的NULL 必须转换为 Char*

You are passing ints to execl, those should all be 0-terminated strings. Also, the final NULL must be cast to Char*.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文