c - Using shared memory with matrices -
i want create matrix in shared memory segment. in second programme can read taillex
, tailley
matrix has '0' in value. create matrix in function initialisersegmem
, values ok... have problem pointers...
my struct mem_share:
typedef struct mem_partage{ int** carte; int tailley; int taillex; }mem_share; mem_share initialiserdonneemem(grille* g){ mem_share mem_share_carte; int x = g->taillex; int y = g->tailley; int i,j; mem_share_carte.carte = malloc(y*sizeof(int*)); for(i=0;i < y;i++){ mem_share_carte.carte[i] = malloc(x*sizeof(int)); } mem_share_carte.carte = g->carte; mem_share_carte.tailley = y; mem_share_carte.taillex = x; return mem_share_carte; } void initialisersegmem(mem_share *mem_share_carte){ int shmid,id_memoire,i,j; int test = 100; int *adresse_mem; key_t cle; cle = 9999; /* creation of shared memory segment */ if(shmget(cle,(int) sizeof(mem_share) ,ipc_creat | ipc_excl | 0777)== -1) { printf("erreur : segment de mémoire partagée existant"); } /*on attache le segment notre espace memoire */ id_memoire = shmget(cle,0,0); adresse_mem = shmat(id_memoire,null,0); /* les 2premiers int seront les tailles y et x*/ printf("tailley %d zzzz",mem_share_carte->tailley); memcpy(adresse_mem,&mem_share_carte->tailley,sizeof(int)); adresse_mem+=sizeof(int); memcpy(adresse_mem,&mem_share_carte->taillex,sizeof(int)); adresse_mem+=sizeof(int); for(i=0;i<mem_share_carte->tailley;i++){ printf("\n"); for(j=0;j<mem_share_carte->taillex;j++){ memcpy(adresse_mem,&(mem_share_carte->carte[i][j]),sizeof(int)); adresse_mem+=sizeof(int); } } }
in second program i'm trying read shared memories:
int shmid,tailley,taillex,y,x; int *adresse_mem; key_t cle; /* on recupere la cle du segment */ cle = 9999; if ((shmid = shmget(cle, 0, 0666)) < 0) { perror("shmget"); exit(1); } /* * on attache le segment notre espace memoire */ if ((adresse_mem = shmat(shmid, null, 0)) == (char *) -1) { perror("shmat"); exit(1); } /* * on lit le segment : * - on recupe la taille y * - puis la taille x * - puis la carte */ memcpy(&tailley,adresse_mem,sizeof(int)); adresse_mem+=sizeof(int); printf("tailley %d",tailley); memcpy(&taillex,adresse_mem,sizeof(int)); printf("taillex %d",taillex); adresse_mem+=sizeof(int); int** carte; carte = malloc(tailley*sizeof(int*)); for(y=0;y<tailley;y++){ carte[y] = malloc(taillex*sizeof(int)); for(x=0;x>taillex;x++){ memcpy(&carte[y][x],adresse_mem,sizeof(int)); adresse_mem+=sizeof(int); } } /* affichage de la carte */ for(y = 0;y<tailley;y++){ printf("\n"); for(x = 0;x<taillex;x++){ printf("%d ",carte[y][x]); } }
by way, in first program must update matrix in shared memories, can call initialisersegmem
new matrix?
a simple example code. child , parent shared 2d vector using system v shared memory segment. sizeelement used offset estimation when data type change. here double used. not error handling on functions' returned code.
#include <stdio.h> #include <unistd.h> #include <sys/shm.h> #include <sys/stat.h> #include <wait.h> unsigned int sizeof_dm(int rows, int cols, size_t sizeelement){ size_t size = rows * (sizeof(void *) + (cols * sizeelement)); return size; } void create_index(void **m, int rows, int cols, size_t sizeelement){ int i; size_t sizerow = cols * sizeelement; m[0] = m+rows; for(i=1; i<rows; i++){ m[i] = (m[i-1]+sizerow); } } void print_matriz(double **matrix, int rows, int cols){ printf("\n"); for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++) printf("%.2f\t",matrix[i][j]); printf("\n"); } } int main(int argc, char **argv){ double **matrix; int cols = 10, rows = 5, shmid; size_t sizematrix = sizeof_dm(rows,cols,sizeof(double)); shmid = shmget(ipc_private, sizematrix, ipc_creat|0600); matrix = shmat(shmid, null, 0); create_index((void*)matrix, rows, cols, sizeof(double)); if(fork()){ int pos=0; for(int i=0; i<rows; i++){ for(int j=0; j<cols; j++) matrix[i][j] = pos++; } wait(null); print_matriz(matrix, rows, cols); //verify child's write shmdt(matrix); shmctl(shmid, ipc_rmid, 0); } else{ sleep(3); //avoid race condition print_matriz(matrix, rows, cols); matrix[2][2] = -99.99; //writing test child shmdt(matrix); } return 0; }
Comments
Post a Comment