Last active
December 10, 2015 09:08
-
-
Save brickgao/4411793 to your computer and use it in GitHub Desktop.
操作系统大作业 分配连续的8M物理内存
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//By Brickgao | |
#include <iostream> | |
#include <queue> | |
#include <cstdio> | |
#include <cstring> | |
#include <cmath> | |
#include <cstdlib> | |
#include <algorithm> | |
#include <vector> | |
#include <ctype.h> | |
#include <sys/mman.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
using namespace std; | |
int main(){ | |
void *st; | |
int tmp1, tmp2, page; | |
page = getpagesize(); | |
printf("Start apply memory...\n"); | |
st = mmap(0, 8 * 1024 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS,-1, 0); | |
if(st == MAP_FAILED){ | |
cout << "Error!\n"; | |
} | |
else{ | |
int maxn = 8 * 1024 * 1024 / page; | |
printf("Successfully apply 8M of memory.\n"); | |
printf("Memory start from: 0X%X\n", st); | |
printf("Memory clean from page(0 ~ %d):", maxn); | |
cin >> tmp1; | |
while(tmp1 < 0 || tmp1 > maxn){ | |
cout << "Error, try again!\n"; | |
cin >> tmp1; | |
} | |
int maxn2 = maxn - tmp1; | |
printf("Clean page(0 ~ %d):", maxn2); | |
cin >> tmp2; | |
while(tmp2 < 0 || tmp2 > maxn2){ | |
cout << "Error, try again!\n"; | |
cin >> tmp2; | |
} | |
if(munmap(st + tmp1 * page, tmp2 * page) == 0){ | |
printf("Memory has been clean from 0X%X to 0X%X.\n", st + tmp1 * page, st + (tmp1 + tmp2) * page); | |
} | |
else{ | |
printf("Memory cleaner error\n"); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment