我正在尝试使用mmap将同一块1MB内存映射到两个堆:
int* pointer_to_physical = (int*) malloc(sizeof(int)*NUM_ELEMENTS_ARRAY);
//Map chunk of 1MB memory to private address space
first_Heap = mmap (pointer_to_physical, sizeof(int) * NUM_ELEMENTS_ARRAY, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
second_Heap = mmap (pointer_to_physical, sizeof(int) * NUM_ELEMENTS_ARRAY, PROT_READ | PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0);
如果我是正确的,他们指向相同的物理内存,但它们位于私有地址空间的不同地址。
我想在写入堆时写入物理内存,然后第二个堆将自动获得值:
first_heap[0]=1980;
printf("second_heap should have the same value: %d",second_heap);
输出:
second_heap should have the same value: 0
为了成为1980年我应该改变什么?