-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmmap_sync.c
63 lines (51 loc) · 1.3 KB
/
mmap_sync.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
62
63
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
int main(int argc, char **argv)
{
int ret;
int fd;
void *addr;
struct stat s;
int file_size = 0;
if (argc != 3) {
printf("%s [filename] [filesize in MB]\n", argv[0]);
return -1;
}
file_size = atoi(argv[2]);
fd = open(argv[1], O_RDWR | O_CREAT, S_IRWXU);
if (fd < 0) {
printf("failed to open %s\n", argv[1]);
return -1;
}
ret = ftruncate(fd, file_size * 1024 * 1024);
if (ret < 0) {
printf("failed to trucate file %s to %d MB\n", argv[1], file_size);
return -1;
}
ret = stat(argv[1], &s);
if (ret < 0) {
printf("failed to stat %s\n", argv[1]);
return -1;
}
addr = mmap(NULL, s.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (!addr) {
printf("Failed to mmap %s\n", strerror(errno));
return -1;
}
printf("data at the address %p is %s\n", addr, addr);
memset(addr, 0, s.st_size);
strcpy(addr, "Hello Mmap");
// sync to the disk sychrnously
msync(addr, s.st_size, MS_SYNC);
perror("msync");
munmap(addr, s.st_size);
close(fd);
return 0;
}