简体   繁体   中英

Multiplicate 2 2D arrays using shared memory

I want to multiply 2 2D arrays (C[i][j]=A[i][j]*B[i][j] ) in C using shared memory . I have to calculate each multiplication separate calling compute in child process .

A=l m matrix and B=u v matrix , so i have to make l*v child processes where i call compute with execv/execl . I get an segmentation fault (core dumped)

main :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    FILE *fp;
    int **A;
    int **B;
    int **C;
    int l,m,u,v,size;
    int pid;

    int segment_id;
    int *shared_memory;
    int i,j,tmp;

    //---------------------------------------------
    //Reading arrays 
    fp=fopen("/home/ubuntu-gnome/Desktop/arrays.txt","r");
    fscanf(fp,"%d %d",&l,&m);   
    *A=(int *)malloc(l*sizeof(int));
    for(i=1;i<=l;i++){
    A[i]=(int *)malloc(m*sizeof(int));
    }
    for(i=1;i<=l;i++){
        for(j=1;j<=m;j++){
            fscanf(fp,"%d ",&tmp);
            A[i][j]=tmp;
        }
     }

    fscanf(fp,"%d %d",&u,&v);   
    *B=(int *)malloc(u*sizeof(int));
     for(i=1;i<=u;i++){
     B[i]=(int *)malloc(v*sizeof(int));
   }
   for(i=1;i<=u;i++){
        for(j=1;j<=v;j++){
            fscanf(fp,"%d",&tmp);
            B[i][j]=tmp;
        }
    }
    fclose(fp);

 //------------------------------------------------------------------
 //printing arrays 
      for(i=1;i<=l;i++){
           for(j=1;j<=m;j++){
               printf("%d  ",A[i][j]);
        }
         printf("\n");
    }
    printf("\n*3");
    for(i=1;i<=u;i++){
         for(j=1;j<=v;j++){
              printf("%d" ,B[i][j]);
        }
        printf("\n");
     }

//---------------------------------------------------------------
//Shared Memory 
    size=sizeof(int)*l*m*u*v*l*v;
    segment_id = shmget(IPC_PRIVATE,size,S_IRUSR|S_IWUSR);
    shared_memory=(int *)shmat(segment_id,NULL,0);
    for(i=1;i<=l;i++)
         for(j=1;j<=m;j++)
             shared_memory[i*j+j]=A[i][j];

    for(i=(l+1);i<=(u+l);i++)
         for(j=(1+m);j<=(v+m);j++)
             shared_memory[i*j+j]=B[i][j];

  //-------------------------------------------------------------------
  // Multiplication error beacuse column of A!= row of B 
    if (u!=m){
         printf("\nMultiplication error !\n");
         exit(1);
    }

//------------------------------------------------------------------
    for (i=1;i<=l;i++){
        for (j=1;j<=v;j++){

            pid=fork();
            if(pid>=0){//fork was successful
                if (pid==0){//child proccess
                    execl("/home/ubuntu-gnome/Desktop/compute","compute", (char)segment_id,(char)size,(char)i,(char)j,(char)m,(char)l,(char)v,NULL);
                }
                else //parent proccess
                    wait(0);
                }
            else // fork failed
            {
                     printf("\n Fork failed, quitting!!!!!!\n");
                     return 1;
            }
        }
     }
  //-------------------------------------------------
  //filing C 
    *C=(int *)malloc(l*sizeof(int));
     for(i=1;i<=l;i++){
           C[i]=(int *)malloc(v*sizeof(int));
      }
     for(i=1;i<=l;i++){
          for(j=1;j<=v;j++){
               B[i][j]=shared_memory[m*(l*v)+i];
          }
     }


//-------------------------------------------------
//printing C=A*B
    for(i=1;i<=l;i++){
        for(j=1;j<=v;j++){
            printf("%d  ",C[i][j]);
        }
        printf("\n");
    }
    /* now detach the shared memory segment */
    shmdt(shared_memory);
    /* now remove the shared memory segment */
    shmctl(segment_id, IPC_RMID, NULL);

    return 0;
}

compute: 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>

int main(int argc,char *argv[])
{
    int segment_id;
    int *shared_memory;
    int key =atoi(argv[0]);
    int size =atoi(argv[1]);
    int row=atoi(argv[2]);
    int col=atoi(argv[3]);
    int m=atoi(argv[4]);
    int l=atoi(argv[5]);
    int v=atoi(argv[6]);
    int i,j,Mul;

    shared_memory=(int *)shmat(segment_id,NULL,0);

    for (i=row;i<=m;i++){
        for (j=col;j<=m;j++){

            Mul=Mul+shared_memory[i]*shared_memory[l*m+j];
            }
            shared_memory[m*(l*v)+i]=Mul;
        Mul=0;
    }

    return 0;
}  

arrays.txt :
6 2 
2 4
4 8
1 0
3 12
7 2
8 9
2 4
2 4 3 12
6 7 8 5

AFAIK, It should be like that,

int **A = (int **)malloc(l * sizeof(int *));
    for (i=0; i<l; i++)
         A[i] = (int *)malloc(m * sizeof(int));

Also, you should start arrays from 0. ( Why? )

for(i=0;i<l;i++){
        for(j=0;j<m;j++){
            fscanf(fp,"%d ",&tmp);
            A[i][j]=tmp;
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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