Answered step by step
Verified Expert Solution
Question
1 Approved Answer
When a file is mmap'd into memory, when is its contents loaded from disk? How can you verify this using 3000test? /* 3000test.c */ 2
When a file is mmap'd into memory, when is its contents loaded from disk? How can you verify this using 3000test?
/* 3000test.c */
2
3
4
5
6
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16
17 void report_error(char *error)
18 {
19 fprintf(stderr, "Error: %s ", error);
20
21 exit(-1);
22 }
23
24 int main(int argc, char *argv[])
25 {
26 struct stat statbuf;
27 char *fn;
28 int fd;
29 size_t len, i, count;
30
31 char *data;
32
33 if (argc < 2) {
34 if (argc < 1) {
35 report_error("no command line");
36 fprintf(stderr, "Usage: %s", argv[0]);
37 } else {
38 report_error("Not enough arguments");
39 fprintf(stderr, "Usage: %s", argv[0]);
40 }
41 }
42
43 fn = argv[1];
44
45 if (stat(fn, &statbuf)) {
46 report_error(strerror(errno));
47 }
48
49 len = statbuf.st_size;
50 printf("File %s: ", fn);
51 printf(" inode %ld ", statbuf.st_ino);
52 printf(" length %ld ", len);
53
54 if (S_ISREG(statbuf.st_mode)) {
55 fd = open(fn, O_RDONLY);
56 if (fd == -1) {
57 report_error(strerror(errno));
58 }
59 data = (char *) mmap(NULL, len,
60 PROT_READ, MAP_SHARED, fd, 0);
61 if (data == MAP_FAILED) {
62 report_error(strerror(errno));
63 }
64
65 count = 0;
66 for (i=0; i 67 if (data[i] == 'a') {68 count++;69 }70 }7172 printf(" a count %ld ", count);7374 if (munmap(data, len) == -1) {75 report_error(strerror(errno));76 }77 close(fd);78 }7980 return 0;81 }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started