Passing a structure with shared memory

Hi I want to pass my structure from father to son in C (not c# or c++). My problem is to do parsing data in shared memory and out share memory , I must to do this i can't use FIFO or other system

you can assume struct operazione and I use an array of this operazione structure[number]

typedef struct {
    int id;
    char operatore[1];
    int operandoprimo;
    int operandosecondo;
    int risultato;
    int risultato;
    int semid;
} operazione;

sorry some comments are in italian but you can understand easily .

father:

 /* CREO ZONA DI MEMORIA */
   int shmid = cMemC(KEY_MEMORIA,righeoperazioni*(sizeof(operazione)),IPC_CREAT|0666);    
    // 

         // attacco memoria condivisa
          shm =attacMemC(shmid,NULL,0);

          s = shm;

          for(exa=0 ; exa<righeoperazioni ; exa++){

                sprintf(*s++,"%d",comandi[exa].id);
                sprintf(*s++,"%s",comandi[exa].operatore);
                sprintf(*s++,"%d",comandi[exa].operandoprimo);
                sprintf(*s++,"%d",comandi[exa].operandosecondo);
                sprintf(*s++,"%d",comandi[exa].risultato);
                sprintf(*s++,"%d",comandi[exa].semid);
                    }
           s=NULL;

    // 

to son :

operazione comandif[righe];

/* CREO ZONA DI MEMORIA */
    int shmid = cMemC(KEY_MEMORIA,oper*(sizeof(operazione)),IPC_EXCL|0666);   
    // attacco memoria condivisa
    shmf =attacMemC(shmid,NULL,0);
    ss = shmf;
    exa=0;
    for(ss=0 ; ss != NULL ; ss++){
                            sprintf(temp,"%d",*ss);
                comandif[exa].id=atoi(temp);
                sprintf(temp,"%d",*ss);
                sprintf(comandif[exa].operatore,"%s",temp);
                sprintf(temp,"%d",*ss);
                comandif[exa].operandoprimo=atoi(temp);
                sprintf(temp,"%d",*ss);
                comandif[exa].operandosecondo=atoi(temp);
                sprintf(temp,"%d",*ss);
                comandif[exa].risultato=atoi(temp);
                sprintf(temp,"%d",*ss);
                comandif[exa].semid=atoi(temp);
                exa++;
           }

Upvotes: 0

Views: 1515

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754060

The for loop using sprintf() to 'write' to the shared memory is bogus. What is the type of s? Its declaration isn't shown, but it doesn't matter much. If it is a char **, as it should be for its use, you are writing multi-digit numbers into 'single bytes' of memory (it's a bit more complex than that, but it bears no resemblance to copying the structure into shared memory as you should be doing).

You probably need:

operazione *target = (operazione *)shm;
memmove(target, commandi, righeoperazioni * sizeof(operazione));

This will give you copies of the commandi array of operazione structures in your shared memory. You actually don't need the target variable; you could use just:

memmove(shm, commandi, righeoperazione * sizeof(operazione));

Upvotes: 1

Related Questions