-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonlinear_mmap.c
More file actions
80 lines (72 loc) · 2.03 KB
/
nonlinear_mmap.c
File metadata and controls
80 lines (72 loc) · 2.03 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Use mmap + the MAP_FIXED flag to map a file in a non-linear way. i.e. page 3, page 1, page 2.
*/
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#define TMP_FILE "/tmp/nonlinear_mmap.tmp"
static void exit_err(char *reason) {
perror(reason);
exit(EXIT_FAILURE);
}
static void pfile(int fd) {
char buf[50];
errno = 0;
while (read(fd, buf, 50) > 0)
write(STDOUT_FILENO, buf, 50);
if (errno != 0)
exit_err("pfile");
puts("");
}
int main(int argc, char *argv) {
int fd;
int psz;
char *map;
void *saddr;
setbuf(stdout, NULL);
psz = sysconf(_SC_PAGESIZE);
unlink(TMP_FILE);
if ((fd = open(TMP_FILE, O_CREAT | O_RDWR, S_IRWXU)) == -1)
exit_err("open");
if (ftruncate(fd, 3 * psz) == -1)
exit_err("ftruncate");
if ((map = mmap(NULL, 3 * psz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == NULL)
exit_err("mmap");
memset(map, 'a', psz);
memset(map + psz, 'b', psz);
memset(map + 2 * psz, 'c', psz);
msync(map, 3 * psz, MS_SYNC);
puts("Contents of file: ");
pfile(fd);
puts("Contents of memory: ");
write(STDOUT_FILENO, map, 3 * psz);
puts("");
saddr = map;
if (munmap(map, 3 * psz) == -1)
exit_err("munmap");
if (mmap(saddr, psz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 2 * psz) == NULL)
exit_err("mmap 1");
if (mmap(saddr + psz, psz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, psz) == NULL)
exit_err("mmap 2");
if (mmap(saddr + 2 * psz, psz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0) == NULL)
exit_err("mmap 3");
puts("Contents of remapped memory: ");
write(STDOUT_FILENO, saddr, 3 * psz);
puts("");
puts("Writing a*psz b*psz c*pcs to remapped memory...");
memset(map, 'a', psz);
memset(map + psz, 'b', psz);
memset(map + 2 * psz, 'c', psz);
msync(map, 3 * psz, MS_SYNC);
// Why isn't the file updated unless we open/close it here? Even though msync has been called.
close(fd);
fd = open(TMP_FILE, O_RDWR);
puts("Contents of file:");
pfile(fd);
exit(EXIT_SUCCESS);
}