-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogical-physical_shm.c
60 lines (52 loc) · 1.51 KB
/
logical-physical_shm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <unistd.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
void vtop(uintptr_t vaddr) {
FILE *pagemap;
intptr_t paddr = 0;
unsigned long offset = (vaddr / sysconf(_SC_PAGESIZE)) * sizeof(uint64_t);
uint64_t e;
if ((pagemap = fopen("/proc/self/pagemap", "r"))) {
if (lseek(fileno(pagemap), offset, SEEK_SET) == offset) {
if (fread(&e, sizeof(uint64_t), 1, pagemap)) {
if (e & (1ULL << 63)) { // page present ?
paddr = e & ((1ULL << 54) - 1); // pfn mask
paddr = paddr * sysconf(_SC_PAGESIZE);
paddr = paddr | (vaddr & (sysconf(_SC_PAGESIZE) - 1));
}
}
}
fclose(pagemap);
}
printf(" %" PRIxPTR , vaddr);
printf(" %" PRIxPTR " \n", paddr);
}
int main(){
const int SIZE = 4096;
const char *name = "mem";
int shm_fd;
int *a;
//print a address
printf("[Processo 1] Enderecos 'a':\n");
vtop((intptr_t)&a);
printf("_______________________________________________\n");
shm_fd = shm_open(name, O_CREAT | O_RDWR, 0666);
ftruncate(shm_fd,SIZE);
a = mmap(0,SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
if (a == MAP_FAILED) {
printf("Map failed\n");
return -1;
}
int pid = fork();
if(pid == 0){
printf("[Processo 2] Enderecos 'a':\n");
vtop((intptr_t)&a);
exit(0);
}
}