-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmap_read.c
80 lines (62 loc) · 2.64 KB
/
mmap_read.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <memory.h>
/* This program shows how mmap() can be used to memory map the text
file present on disk into process's Virtual address space */
static void breakpoint () {}
typedef struct identity {
char name[128];
char city[128];
} friend_t;
int
main(int argc, char *argv[]){
if(argc < 2){
printf("File path not mentioned\n");
exit(0);
}
const char *filepath = argv[1];
/* OPen the file in Read-Write OMode */
int fd = open(filepath, O_RDWR );
if(fd < 0){
printf("\n\"%s \" could not open\n",
filepath);
exit(1);
}
/* Extract the size of the file */
struct stat statbuf;
int err = fstat(fd, &statbuf);
if(err < 0){
printf("\n\"%s \" could not open\n",
filepath);
exit(2);
}
char *ptr = mmap(NULL, /*Let Kernel decide the starting address of mapping in Virtual Memory */
statbuf.st_size, /* Memory size to Map */
PROT_READ | PROT_WRITE, /* Map the memory in Read-Write mode, meaning the Virtual Memory can be read
and written by the process. Note that, this permission must be compatible
with permissions with which the file is open using open () */
MAP_SHARED, /* To see the changes in the file, should be always MAP_SHARED. */
fd, /* FD represents the identifier of the external data source, in this case it is a file */
0); /* Offset into the text file from which the mapping has to begin, pass zero to map from the
beginning of the file */
if(ptr == MAP_FAILED){
printf("Mapping Failed, errorno = %d\n", errno);
return 1;
}
close(fd); /* We will not going to read/write into file using file-handling routines, hence close the fd. mmap can still
use this file even if its fd is closed */
friend_t friend1 = { "Ana", "Dallas"};
memcpy (ptr, &friend1, sizeof(friend1));
msync(ptr, sizeof(friend1), MS_SYNC);
err = munmap(ptr, statbuf.st_size); /* Destroy the mapping once done */
if(err != 0){
printf("UnMapping Failed\n");
return 1;
}
return 0;
}