-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.c
61 lines (39 loc) · 1.15 KB
/
test2.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
60
61
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#define SLEEP_USEC 1000000
#define MMAP_SIZE 4096
#define NUM_MAPS 20
int main () {
void * maps[NUM_MAPS];
while (1) {
int j;
for (j = 0; j < NUM_MAPS; j++) {
maps[j] = mmap(NULL, MMAP_SIZE, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (maps[j] == MAP_FAILED) {
perror("Could not map region\n");
return -1;
}
// read the page
printf("x=%p\n", (void*)(*(unsigned long*)x));
usleep(SLEEP_USEC);
madvise(maps[j], MMAP_SIZE, MADV_RANDOM);
printf("mapped page at %p\n", maps[j]);
usleep(SLEEP_USEC);
/* dirty the page */
for (int i = 0; i < 4096; i++) {
char * y = (char*)&maps[j];
y[i] = 'a';
}
usleep(SLEEP_USEC);
}
/* now free them */
for (j = 0; j < NUM_MAPS; j++) {
munmap(maps[j], MMAP_SIZE);
}
usleep(SLEEP_USEC);
}
}