-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_2.c
122 lines (105 loc) · 2.6 KB
/
mem_2.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* This code does the first task which is as follows:
* a) allocating a massive array with malloc and initializing it
*
**/
#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
// Defining the variables
#define MEM_RW 0
#define MEM_RO 1
#define MEM_NO 2
struct memregion {
void *from;
void *to;
unsigned char mode; /* MEM_RW, or MEM_RO, or MEM_NO */
};
extern int get_mem_layout (struct memregion *regions, unsigned int size);
/**
* Prints out the memory regions found.
**/
void printRegions(struct memregion *memList, unsigned int size){
for(int i = 0; i < size; i++){
printf("0x%08x - ", (int)memList[i].from);
printf("0x%08x ", (int)memList[i].to);
switch(memList[i].mode){
case MEM_NO:
printf("NO\n");
break;
case MEM_RO:
printf("RO\n");
break;
case MEM_RW:
printf("RW\n");
break;
}
}
printf("\n");
}
int main()
{
int err;
int size = 22; // Size of the regions
// Memory regions for the first and the second
struct memregion *memList = malloc(size * sizeof(struct memregion) );
struct memregion *memList2 = malloc(size * sizeof(struct memregion) );
// Doing the first scan
printf("Doing the first scan:\n");
printf("There are %d regions.\n", get_mem_layout(memList, size));
printRegions(memList, size);
// Using mmap
// Opening the file, creates if not exists
const char *filepath = "bigfile.txt";
int fd = open(filepath, O_RDWR | O_CREAT , S_IRUSR | S_IWUSR);
if(fd == -1){
printf("\n\"%s \" exists.\n", filepath);
exit(1);
}else{
// Writes to the file to make a big file.
for(int i = 0; i < 0x3000; i++){
err = write(fd, "hello there\n", 12);
if(err < 0){
printf("\n\"%s \" could not be created.\n",
filepath);
exit(1);
}
}
}
struct stat statbuf;
err = fstat(fd, &statbuf);
if(err < 0){
printf("\n\"%s \" could not open\n", filepath);
exit(2);
}
char *ptr = mmap(
NULL,
statbuf.st_size,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fd, 0);
if(ptr == MAP_FAILED){
printf("Mapping Failed\n");
return 1;
}
close(fd);
// Second scan in between
printf("Doing the second scan:\n");
printf("There are %d regions.\n", get_mem_layout(memList2, size));
printRegions(memList2, size);
printf("Location of big file mmap: %p\n", ptr);
// Unmapping the file
err = munmap(ptr, statbuf.st_size);
if(err != 0){
printf("UnMapping Failed\n");
return 1;
}
// Deallocate regions
// free(memList);
return 0;
}