Closed Julien00859 closed 8 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).
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.
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
Segmentation fault (core dumped)
:'(
Le plus étonnant c'est que le process père y accède correctement avec le même code
My bad, tu peux pas faire des pilotes\ dans les shared mem :(
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
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
Résolu. Je ferme.
Every pilote access the same structure instead of accessing its one.