izanagi1995 / Projet_C_F1

On va gérer <3
0 stars 2 forks source link

Pilote's side shared memory #5

Closed Julien00859 closed 7 years ago

Julien00859 commented 7 years ago

Every pilote access the same structure instead of accessing its one.

/* Attach the shared memory and get our structure */
pilote* shm_addr = (pilote*) shmat(shm_id, NULL, 0);
pilote myself = shm_addr[car_idx]; // car_idx is different in each child
printf("Address: %p\n", &myself); // Give the same address in each child
izanagi1995 commented 7 years ago

It's normal, the shared memory starts at the same address, but we offset it to get the right part. Example Mem starts at 0x123abc (for every pilot) length 0x210000 So each pilot starts at mem_start + (mem_length/num_cars*car_idx) The shared memory has the same address but the pilote struct not. To prove it, make a test (modify one pilot and see the others intact).

Julien00859 commented 7 years ago

I reword my problem: when I try to access a specific structure within the shared memory I always get the same address instead of getting an unique address specific to the structure I access.

As I casted the void pointer to a struct pilote pointer I thought I was able to access each structure just like if it was a an array (that's how I deal with pids, cars and pipes malloc elsewhere) but there is something related to the shared memory that forbid me to do the same.

In the example given before, car_idx goes from 0 to the number of cars.

izanagi1995 commented 7 years ago

Got it!

pilote* shm_addr = (pilote*) shmat(shm_id, NULL, 0);

You cast the shared memory (that is an in-memory arry struct so each item are also pointers).

So I think that the first Var alloc must be of type pilote* not pilote

Julien00859 commented 7 years ago

Segmentation fault (core dumped) :'(

Julien00859 commented 7 years ago

Le plus étonnant c'est que le process père y accède correctement avec le même code

izanagi1995 commented 7 years ago

My bad, tu peux pas faire des pilotes\ dans les shared mem :(

izanagi1995 commented 7 years ago

Je vais reprendre ton code plus attentivement, je viens de faire des tests et c'est vrai que même dans une shm, tu as des addresses distinctes par éléments

izanagi1995 commented 7 years ago

Trouvé... C'est une connerie!

pilote* shm_addr = (pilote*) try_sys_call_ptr(shmat(shm_id, NULL, 0), "Shmat failure");
pilote myself = shm_addr[car_idx];
printf("ADD : %p\r\n", &shm_addr[i]);

Retourne

ADD : 0x10c005020
Process ID 3951 is car at index 1 and has access to 0x7fff53c31ae0.
ADD : 0x10c005040
Process ID 3952 is car at index 2 and has access to 0x7fff53c31ae0.
ADD : 0x10c005060
Process ID 3953 is car at index 3 and has access to 0x7fff53c31ae0.

Tout simplement car &shm_addr[i] permet de trouver l'adresse du pilote DANS LA SHM Alors que &myself permet de trouver l'adresse du pilote DANS LA MEMOIRE DU PROCESSUS FILS

izanagi1995 commented 7 years ago

Résolu. Je ferme.