简体   繁体   English

如何诊断使用共享 memory 的 C 中的矩阵错误?

[英]How to diagnose matrix error in C which utilises shared memory?

I am trying to create a program that can multiply 2 2x2 matrixes using separate processes ( child1 and child2 ).我正在尝试创建一个程序,该程序可以使用单独的进程( child1child2 )将 2 个2x2矩阵相乘。 Specifically child 1 processes row1 of the resulting matrix and child2 processes row 2 of the resulting matrix.具体来说, child 1 处理结果矩阵的第 1 行,而row1处理结果矩阵child2第 2 row This resulting matrix is stored in shared memory.该结果矩阵存储在共享 memory 中。

However during the testing of child1 which is yet to be completed, it is expected to write to the top row ( row1 ) of the resulting matrix but it is showing that the nested loop is writing 660 into the second row as shown below.然而,在尚未完成的child1测试期间,预计将写入结果矩阵的顶行 ( row1 ),但它显示嵌套循环正在将660写入第二行,如下所示。

Resulting Matrix2 from code
220              660     
660               0 

Expected matrix from child1来自child1的预期矩阵

  220     660     
    0     0 

test.c测试.c

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <stdbool.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

#include "shm_com.h"


//signal handler should be void of return type
void sig_handler(int signo){
    printf("Inside handler function\n");
}

int main(){
    int PROCESS_COUNT = 0;
    bool display = false;
    int row, column, limit=2;
    int M[2][2] = {     {20, 20,},
                    {10, 6,}};
    int N[2][2] = {     {10, 30,},
                    {1, 3,}};
    int Matrix_Size1=sizeof(M)/sizeof(int);
    int Matrix_Size2=sizeof(N)/sizeof(int);
    if(Matrix_Size1==Matrix_Size2) printf("Matrixes are the same size\n");
    

    int shmid;
    void *shared_memory = (void *)0;
    struct shared_use_st *shared_stuff;
    shmid = shmget((key_t)9998, sizeof(struct shared_use_st), 0666 | IPC_CREAT);
    //Creation of Shared Memory
    if (shmid == -1) {
        fprintf(stderr, "shmget failed\n");
        exit(EXIT_FAILURE);
    }
    
    pid_t pid1,pid2;
    pid1 = fork();
    PROCESS_COUNT = 1;
    if(pid1!=0){
        pid2 = fork();
        PROCESS_COUNT = 2;
    }
    //Warning code after this point will be included into child unless in if statement

    if ((PROCESS_COUNT == 1 && pid1==0) || (PROCESS_COUNT == 2 && pid2 == 0)){
        /* child1 ------------------------------------------------------------*/
        if(PROCESS_COUNT == 1){
            printf("Child 1 Process: working with Q's 1 row\n");
            //sleep(10);
            //Get access to shared memory
            shared_memory = shmat(shmid, (void *)0, 0);
            if (shared_memory == (void *)-1) {
                fprintf(stderr, "shmat failed\n");
                exit(EXIT_FAILURE);
            }
            if(display){printf("Child Memory attached at %X\n", (int)shared_memory);}
            shared_stuff = (struct shared_use_st *)shared_memory;
            for(column=0; column<limit; column++){
                for (row=0; row<limit; row++){
                
                    shared_stuff->Q_matrix[0][column] += M[0][column]*N[row][column];
                    printf("check %d   ",shared_stuff->Q_matrix[0][column]);
                    //printf("check Row:%d,Column: %d   ",row,column);
                }
                if(display){printf("Row1_Q_matrix \n");}
                printf("\n");
                
            }
        //kill(pid1, SIGALRM);
        exit(1);
        }
        /*-------------------------------------------------------------------*/
        /* child2 */
        else if(PROCESS_COUNT == 2){
            printf("Child 2 Process: working with Q's 2 row\n");
            //Get access to shared memory
            shared_memory = shmat(shmid, (void *)0, 0);
            if (shared_memory == (void *)-1) {
                fprintf(stderr, "shmat failed\n");
                exit(EXIT_FAILURE);
            }
            if(display){printf("Child Memory attached at %X\n", (int)shared_memory);}
            shared_stuff = (struct shared_use_st *)shared_memory;
    
        }
    } else {
        /* parent ------------------------------------------------------------*/    
        if(display){printf("Parent pid: %i PROCESS_COUNT: %i \n  ", getpid(),PROCESS_COUNT);}

        //Get access to shared memory
        shared_memory = shmat(shmid, (void *)0, 0);
        if (shared_memory == (void *)-1) {
            fprintf(stderr, "shmat failed\n");
            exit(EXIT_FAILURE);
        }
        if(display){printf("Parent Memory attached at %X\n", (int)shared_memory);}
        shared_stuff = (struct shared_use_st *)shared_memory;

        //Write number to shared memory------------------------------------------------*/
        
        
        
        //Write and Display M matrix
        for (row=0; row<limit; row++)
        {
            for(column=0; column<limit; column++){
                shared_stuff->M_matrix[row][column] = M[row][column];
                if(display){printf("%d     ", (shared_stuff->M_matrix[row][column]));}
            }
            if(display){printf("M_matrix \n");}
            printf("\n");
            
        }
        //Write and Display N matrix
        for (row=0; row<limit; row++)
        {
            for(column=0; column<limit; column++){
                shared_stuff->N_matrix[row][column] = N[row][column];
                if(display){printf("%d     ", (shared_stuff->N_matrix[row][column]));}
            }
            if(display){printf("N_matrix \n");}
            printf("\n");
         }
        //Write and Display Q matric
        printf("Written Matrix\n");
        for (row=0; row<limit; row++)
        {
            for(column=0; column<limit; column++){
                shared_stuff->Q_matrix[row][column] = 0;
                printf("%d     ", (shared_stuff->Q_matrix[row][column]));
            }
            if(display){printf("Q_matrix");}
            printf("\n");
         }
        wait(NULL);
        sleep(10);
        printf("Resulting Matrix2\n");
        printf("%d     ", (shared_stuff->Q_matrix[0][0]));
        printf("%d     \n", (shared_stuff->Q_matrix[0][1]));
        printf("%d     ", (shared_stuff->Q_matrix[1][0]));
        printf("%d     \n", (shared_stuff->Q_matrix[1][1]));
        /*for (row=0; row<limit; row++){
            for(column=0; column<limit; column++){
                printf("%d     ", (shared_stuff->Q_matrix[row][column]));
            }
            if(display){printf("Q_matrix");}
            printf("\n");
         }*/
    exit(EXIT_SUCCESS);
    printf("done\n");
    }
    
}

Here is my structure of shared memory:这是我共享 memory 的结构:

#define TEXT_SZ 2048
struct shared_use_st {
    int M_matrix[1][1];
    int N_matrix[1][1];
    int Q_matrix[1][1];
};

Can someone explain what i am doing wrong?有人可以解释我做错了什么吗? It looks like the memory address for spot Q_matrix[0][1] is the same as the Q_matrix[1][0]?看起来 Spot Q_matrix[0][1] 的 memory 地址与 Q_matrix[1][0] 相同?

Both Q_matrix[0][1] and Q_matrix[1][0] are invalid; Q_matrix[0][1]Q_matrix[1][0]都无效; the "matrix" is a 1x1 matrix, so the only valid index is Q_matrix[0][0] . “矩阵”是一个 1x1 矩阵,因此唯一有效的索引是Q_matrix[0][0]

Regardless, because the second dimension is only size 1, you'd expect the indexing to produce the same address.无论如何,因为第二个维度的大小只有 1,所以您希望索引生成相同的地址。 In one case, you're asking for an address one int later, in the other, for an address one int[1] later, but an int and an int[1] are the same size, so the addressing gets the same result.在一种情况下,您要求稍后一个int的地址,在另一种情况下,要求稍后一个int[1]的地址,但是intint[1]的大小相同,因此寻址得到相同的结果.

I suspect you intended to declare a Q_matrix[2][2] (for which 0 and 1 are legal indices in both dimensions), not Q_matrix[1][1] (for which 0 is the only legal index in either dimension).我怀疑您打算声明一个Q_matrix[2][2] (其中01是两个维度中的合法索引),而不是Q_matrix[1][1] (其中0是任一维度中唯一的合法索引)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM