我想在C中使用共享内存乘以2个2D数组(C [i] [j] = A [i] [j] * B [i] [j])。我必须在子进程中计算每个乘法单独调用compute。
A = l m矩阵和B = u v矩阵,所以我必须进行l * v子进程,其中我用execv / execl调用compute。我遇到了分段错误(核心转储)
主要:
#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
答案 0 :(得分:0)
AFAIK,应该是这样,
int **A = (int **)malloc(l * sizeof(int *));
for (i=0; i<l; i++)
A[i] = (int *)malloc(m * sizeof(int));
此外,您应该从0开始数组(Why?)
for(i=0;i<l;i++){
for(j=0;j<m;j++){
fscanf(fp,"%d ",&tmp);
A[i][j]=tmp;
}
}